<?php
namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\BidModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractNeiListModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiModel;
use app\index\model\DeviceJieModel;
use app\index\model\DeviceLinModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\EnginBondKaiModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginBudshouModel;
use app\index\model\EnginBudsubModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\IncomeEnginModel;
use app\index\model\IncomeHuanModel;
use app\index\model\LabourLingModel;
use app\index\model\LabourShenModel;
use app\index\model\PaybaoModel;
use app\index\model\PaydataModel;
use app\index\model\PaymentBaoListModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentDayListModel;
use app\index\model\PaymentDayModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongListModel;
use app\index\model\PaymentGongModel;
use app\index\model\PaymentJieModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PaymentNeikouListModel;
use app\index\model\PaymentNeikouModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouModel;
use app\index\model\StuffCaiModel;
use app\index\model\StuffJieModel;
use app\index\model\StuffRuModel;
use app\index\model\SubbaoShenModel;
use app\index\model\UserModel;
use app\index\service\BidService;
use app\index\service\BudgetService;
use app\index\service\ContractService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\IncomeService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PaymentService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Payment extends Common
{

    public function list_payment_engin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['pay.deleted_user','=', 0];
        $where[] = ['pay.comid','=',$this->userinfo['comid']];
        $where[] = ['pay.isturn','=',1];
        $where[] = ['pay.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['pay.enginid','=', $param['enginid']];
        }
        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['pay.caiid','=', $param['caiid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|pay.number|pay.zhai|pay.money|pay.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['pay.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['budid']) && !empty($param['budid'])){
            $where[] = ['pay.budid','=', $param['budid']];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['pay.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['pay.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['pay.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['pay.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['pay.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['pay.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['pay.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['pay.status','=', $param['status']];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['budid'] = (new PaymentService())->get_payment_budid();
        $data['is_budid'] = isset($param['budid']) ? $param['budid'] : '';

        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $data['data'] = PaymentEnginModel::alias('pay')
            ->field('pay.*,e.name as enginname,cai.name as cainame')
            ->join('engin e','e.id=pay.enginid')
            ->leftjoin('contract_cai cai','cai.id=pay.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('payment_engin', 'pay'))
            ->order('pay.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new PaymentService())->get_payment_budid_name($item['budid']);
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>3,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('payment_engin');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['yiju'])){
                return outputJson(4000, '请选择付款依据');
            }else{
                if($param['yiju'] != 'none'){
                    if(empty($param['yijuid'])){
                        return outputJson(4000, '请选择依据(名称)');
                    }
                }
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入请款金额');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请选择付款对象');
            }



            $param['number'] = (new PaymentService())->create_payment_engin_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['isturn'] = 1;
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = PaymentEnginModel::insertGetId($param);
                (new OptService())->relation_file($files,'payment_engin',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_engin', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_engin', $id, '项目付款(供应商)');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['budid'] = (new PaymentService())->get_payment_budid();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_payment_engin_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['yiju'])){
                return outputJson(4000, '请选择付款依据');
            }else{
                if($param['yiju'] != 'none'){
                    if(empty($param['yijuid'])){
                        return outputJson(4000, '请选择依据(名称)');
                    }
                }
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入请款金额');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请选择付款对象');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                PaymentEnginModel::update($param);
                (new OptService())->relation_file($files,'payment_engin',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_engin', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_engin', $param['id'], '项目付款(供应商)');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentEnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $yijuidInfo = [];
            if($data['data']['yiju'] == 'concaistuff'){
                $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '材料合同';
            }elseif($data['data']['yiju'] == 'stuffcai'){
                $yijuidInfo = StuffCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                $yijuidInfo['stname'] = '材料采购单';
            }elseif($data['data']['yiju'] == 'stuffru'){
                $yijuidInfo = StuffRuModel::where(['id'=>$data['data']['yijuid']])->find();
                 $yijuidInfo['stname'] = '材料入库单';
            }elseif($data['data']['yiju'] == 'stuffjie'){
                $yijuidInfo = StuffJieModel::where(['id'=>$data['data']['yijuid']])->find();
                 $yijuidInfo['stname'] = '材料结算单';
            }elseif($data['data']['yiju'] == 'concailabour'){
                $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                 $yijuidInfo['stname'] = '劳务合同';
            }elseif($data['data']['yiju'] == 'labourshen'){
                $yijuidInfo = LabourShenModel::where(['id'=>$data['data']['yijuid']])->find();
                 $yijuidInfo['stname'] = '劳务进度款申报';
                 $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
            }elseif($data['data']['yiju'] == 'labourling'){
                $yijuidInfo = LabourLingModel::where(['id'=>$data['data']['yijuid']])->find();
                 $yijuidInfo['stname'] = '零星劳务单';
            }elseif($data['data']['yiju'] == 'concaidevice'){
                $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                 $yijuidInfo['stname'] = '租赁合同';
            }elseif($data['data']['yiju'] == 'devicejie'){
                $yijuidInfo = DeviceJieModel::where(['id'=>$data['data']['yijuid']])->find();
                 $yijuidInfo['stname'] = '租赁结算';
                $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
            }elseif($data['data']['yiju'] == 'devicelin'){
                $yijuidInfo = DeviceLinModel::where(['id'=>$data['data']['yijuid']])->find();
                 $yijuidInfo['stname'] = '机械费登记';
            }elseif($data['data']['yiju'] == 'concaisubbao'){
                $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                 $yijuidInfo['stname'] = '分包合同';
            }elseif($data['data']['yiju'] == 'subbaoshen'){
                $yijuidInfo = SubbaoShenModel::where(['id'=>$data['data']['yijuid']])->find();
                 $yijuidInfo['stname'] = '分包进度款申报';
            }elseif($data['data']['yiju'] == 'concaiqi'){
                $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
                 $yijuidInfo['stname'] = '其他采购合同';
                $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
            }
            $data['yujuinfo'] = $yijuidInfo;


            $data['budid'] = (new PaymentService())->get_payment_budid();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('payment_engin', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_engin(Request $request){
        $param = $request->post();
        $data = PaymentEnginModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PaymentEnginModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'payment_engin', $data['id'], '项目付款(供应商)');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_payment_engin(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PaymentEnginModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['budid_name'] = (new PaymentService())->get_payment_budid_name($data['data']['budid']);

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $data['engininfo'] = $engininfo;


        $yijuidInfo = [];
        $yijuidInfo['stname'] = '无';
        if($data['data']['yiju'] == 'concaistuff'){
            $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '材料合同';
        }elseif($data['data']['yiju'] == 'stuffcai'){
            $yijuidInfo = StuffCaiModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '材料采购单';
        }elseif($data['data']['yiju'] == 'stuffru'){
            $yijuidInfo = StuffRuModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '材料入库单';
        }elseif($data['data']['yiju'] == 'stuffjie'){
            $yijuidInfo = StuffJieModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '材料结算单';
        }elseif($data['data']['yiju'] == 'concailabour'){
            $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '劳务合同';
        }elseif($data['data']['yiju'] == 'labourshen'){
            $yijuidInfo = LabourShenModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '劳务进度款申报';
            $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
        }elseif($data['data']['yiju'] == 'labourjie'){
            $yijuidInfo = LabourJieModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '劳务完工结算';
            $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
        }elseif($data['data']['yiju'] == 'labouryong'){
            $yijuidInfo = LabourYongModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '劳务用工单';
            $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
        }elseif($data['data']['yiju'] == 'labourling'){
            $yijuidInfo = LabourLingModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '零星劳务单';
        }elseif($data['data']['yiju'] == 'concaidevice'){
            $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '租赁合同';
        }elseif($data['data']['yiju'] == 'devicejie'){
            $yijuidInfo = DeviceJieModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '租赁结算';
            $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['caiid']])->value('gonger');
        }elseif($data['data']['yiju'] == 'devicelin'){
            $yijuidInfo = DeviceLinModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '机械费登记';
        }elseif($data['data']['yiju'] == 'concaisubbao'){
            $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '分包合同';
        }elseif($data['data']['yiju'] == 'subbaoshen'){
            $yijuidInfo = SubbaoShenModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '分包进度款申报';
        }elseif($data['data']['yiju'] == 'subbaojie'){
            $yijuidInfo = SubbaoJieModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '分包进度款申报';
        }elseif($data['data']['yiju'] == 'concaiqi'){
            $yijuidInfo = ContractCaiModel::where(['id'=>$data['data']['yijuid']])->find();
            $yijuidInfo['stname'] = '其他采购合同';
            $yijuidInfo['gonger'] = ContractCaiModel::where(['id'=>$yijuidInfo['id']])->value('gonger');
        }
        $data['yujuinfo'] = $yijuidInfo;


        $data['data']['accmoney'] = AccountListModel::where(['type'=>3,'mid'=>$data['data']['id']])->sum('money');


        $data['data']['payfo_status'] = (new PaymentService())->get_paydata_paystatus_name($data['data']['money'], $data['data']['payfee']);
        $data['data']['payfo_payfee'] = $data['data']['payfee'];
        $data['data']['payfo_dt'] = AccountListModel::where(['type'=>3,'mid'=>$data['data']['id']])->order('dt DESC')->value('dt');
        $data['data']['payfo_dai'] = $data['data']['money'] - $data['data']['payfee'];


        $data['data']['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $data['data']['incqing']);
        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('payment_engin', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_payment_engin_kuan(Request $request){
        $param = $request->post();
        get_page($param);

        $whereZhu[] = ['pay.deleted_user','=', 0];
        $whereZhu[] = ['pay.comid','=',$this->userinfo['comid']];
        $whereZhu[] = ['pay.isturn','=',1];
        $whereZhu[] = ['pay.status','in',[0,1]];

        $where = [];
        if(isset($param['my']) && $param['my'] > 0){
            $where[] = ['pay.created_user','=', $this->userinfo['id']];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['pay.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|pay.number|pay.zhai|pay.money|pay.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['pay.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['budid']) && !empty($param['budid'])){
            $where[] = ['pay.budid','=', $param['budid']];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['pay.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['pay.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['pay.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['pay.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['pay.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['pay.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['pay.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['pay.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $data['budid'] = (new PaymentService())->get_payment_budid();
        $data['is_budid'] = isset($param['budid']) ? $param['budid'] : '';

        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        if(isset($param['payss']) && !empty($param['payss'])){
            if($param['payss'] == 'no'){
                $where[] = ['pay.payfee','=', 0];
            }
            if($param['payss'] == 'bu'){
                $where[] = ['pay.payfee','neq', 0];
                $where[] = Db::raw('pay.payfee < pay.money');
            }
            if($param['payss'] == 'quan'){
                $where[] = ['pay.payfee','neq', 0];
                $where[] = Db::raw('pay.payfee >= pay.money');
            }
        }
        $leftarr = [];
        $leftarr['no'] = PaymentEnginModel::alias('pay')
            ->field('pay.*,e.name as enginname')
            ->join('engin e','e.id=pay.enginid')
            ->where($whereZhu)
            ->where('pay.payfee','=',0)
            ->where((new MenuService())->get_miss_where('payment_engin_kuan', 'e'))
            ->count();
        $leftarr['bu'] = PaymentEnginModel::alias('pay')
            ->field('pay.*,e.name as enginname')
            ->join('engin e','e.id=pay.enginid')
            ->where($whereZhu)
            ->where('pay.payfee < pay.money')
            ->where('pay.payfee','neq',0)
            ->where((new MenuService())->get_miss_where('payment_engin_kuan', 'e'))
            ->count();


        $leftarr['quan'] = PaymentEnginModel::alias('pay')
            ->field('pay.*,e.name as enginname')
            ->join('engin e','e.id=pay.enginid')
            ->where($whereZhu)
            ->where('pay.payfee >= pay.money')
            ->where('pay.payfee','neq',0)
            ->where((new MenuService())->get_miss_where('payment_engin_kuan', 'e'))
            ->count();
        $leftarr['all'] = $leftarr['no'] + $leftarr['bu'] + $leftarr['quan'];
        $data['leftarr'] = $leftarr;


        $data['data'] = PaymentEnginModel::alias('pay')
            ->field('pay.*,e.name as enginname')
            ->join('engin e','e.id=pay.enginid')
            ->where($whereZhu)
            ->where($where)
            ->where((new MenuService())->get_miss_where('payment_engin_kuan', 'e'))
            ->order('pay.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new PaymentService())->get_payment_budid_name($item['budid']);
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>3,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('payment_engin_kuan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_payment_engin_kuan_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.type','=',3];
        $where[] = ['list.mid','=',$param['mid']];


        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=list.accid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budname'] = (new PaymentService())->get_payment_budid_name($item['budid']);
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();

        $data['midinfo'] = PaymentEnginModel::where(['id'=>$param['mid']])->find();
        if($data['midinfo']){
            $data['midinfo']['enginname'] = EnginModel::where(['id'=>$data['midinfo']['enginid']])->value('name');
            $data['midinfo']['budid_name'] = (new PaymentService())->get_payment_budid_name($data['midinfo']['budid']);
            $data['midinfo']['accmoney'] = AccountListModel::where(['type'=>3,'mid'=>$data['midinfo']['id']])->sum('money');
            $data['midinfo']['daimoney'] = $data['midinfo']['money'] - $data['midinfo']['accmoney'];
        }
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_engin_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $payinfo = PaymentEnginModel::where(['id'=>$param['id']])->find();

            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>3,'mid'=>$payinfo['id']])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['comid'] = $this->userinfo['comid'];
            $accInfo['type'] = 3;
            $accInfo['mid'] = $param['id'];
            $accInfo['enginid'] = $payinfo['enginid'];
            $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            $accInfo['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $accInfo['money'] = $param['fumoney'];
            $accInfo['budid'] = $payinfo['budid'];
            $accInfo['zhai'] = $payinfo['zhai'];
            $accInfo['faer'] = $payinfo['gonger'];
            $accInfo['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payinfo['incdaotype']);
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['bankname'] = $payinfo['bankname'];
            $accInfo['banknum'] = $payinfo['banknum'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['created_user'] = $this->userinfo['id'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['created_at'] = date("Y-m-d H:i:s");
            $accInfo['updated_at'] = date("Y-m-d H:i:s");



            Db::startTrans();
            try {
                PaymentEnginModel::where(['id'=>$param['id']])->update(['incqing'=>$param['incqing']]);

                $id = AccountListModel::insertGetId($accInfo);

                $payfee = AccountListModel::where(['type'=>3,'mid'=>$param['id']])->sum('money');
                PaymentEnginModel::where(['id'=>$param['id']])->update(['payfee'=>$payfee]);

                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentEnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['data']['budid_name'] = (new PaymentService())->get_payment_budid_name($data['data']['budid']);
            $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
            $data['data']['accmoney'] = AccountListModel::where(['type'=>3,'mid'=>$data['data']['id']])->sum('money');
            $data['data']['nomoney'] = round($data['data']['money'] - $data['data']['accmoney'], 2);


            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_payment_engin_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AccountListModel::where(['id'=>$param['id']])->find();
            $payinfo = PaymentEnginModel::where(['id'=>$old['mid']])->find();

            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>3,'mid'=>$payinfo['id']])->where('id','neq',$param['id'])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }


            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['id'] = $param['id'];
            $accInfo['enginid'] = $payinfo['enginid'];
            $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            $accInfo['money'] = $param['fumoney'];
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['updated_at'] = date("Y-m-d H:i:s");


            Db::startTrans();
            try {
                PaymentEnginModel::where(['id'=>$payinfo['id']])->update(['incqing'=>$param['incqing']]);

                AccountListModel::update($accInfo);

                $payfee = AccountListModel::where(['type'=>3,'mid'=>$payinfo['id']])->sum('money');
                PaymentEnginModel::where(['id'=>$payinfo['id']])->update(['payfee'=>$payfee]);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $payment = PaymentEnginModel::where(['id'=>$data['data']['mid']])->find();
            $payment['budid_name'] = (new PaymentService())->get_payment_budid_name($payment['budid']);
            $payment['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payment['incdaotype']);
            $payment['accmoney'] = AccountListModel::where(['type'=>3,'mid'=>$payment['id']])->sum('money');
            $payment['nomoney'] = $payment['money'] - $payment['accmoney'];
            $data['payment'] = $payment;

            $engininfo = EnginModel::get_info_by_pk($payment['enginid']);
            $data['engininfo'] = $engininfo;

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_engin_kuan(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }



    public function list_payment_bao(Request $request){
        $param = $request->post();
        get_page($param);

        $whereZhu[] = ['bao.deleted_user','=', 0];
        $whereZhu[] = ['bao.comid','=',$this->userinfo['comid']];
        $whereZhu[] = ['bao.isturn','=',1];
        $whereZhu[] = ['bao.status','in',[0,1]];

        $where = [];
        if(isset($param['my']) && $param['my'] > 0){
            $where[] = ['bao.created_user','=', $this->userinfo['id']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|bao.number|bao.zhai|bao.money|bao.gonger|bao.banknum|bao.bankname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bao.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['bao.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['bao.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['bao.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['bao.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['bao.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['bao.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['bao.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bao.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';




        if(isset($param['payss']) && !empty($param['payss'])){
            if($param['payss'] == 'no'){
                $where[] = ['bao.payfee','=', 0];
            }
            if($param['payss'] == 'bu'){
                $where[] = ['bao.payfee','neq', 0];
                $where[] = Db::raw('bao.payfee < bao.money');
            }
            if($param['payss'] == 'quan'){
                $where[] = ['bao.payfee','neq', 0];
                $where[] = Db::raw('bao.payfee >= bao.money');
            }
        }
        $leftarr = [];
        $leftarr['no'] = PaymentBaoModel::alias('bao')
            ->field('bao.*,e.name as enginname')
            ->leftJoin('engin e','e.id=bao.enginid')
            ->where($whereZhu)
            ->where((new MenuService())->get_miss_where('payment_bao', 'bao'))
            ->where('bao.payfee','=',0)
            ->count();
        $leftarr['bu'] = PaymentBaoModel::alias('bao')
            ->field('bao.*,e.name as enginname')
            ->leftJoin('engin e','e.id=bao.enginid')
            ->where($whereZhu)
            ->where((new MenuService())->get_miss_where('payment_bao', 'bao'))
            ->where('bao.payfee < bao.money')
            ->where('bao.payfee','neq',0)
            ->count();
        $leftarr['quan'] = PaymentBaoModel::alias('bao')
            ->field('bao.*,e.name as enginname')
            ->leftJoin('engin e','e.id=bao.enginid')
            ->where($whereZhu)
            ->where((new MenuService())->get_miss_where('payment_bao', 'bao'))
            ->where('bao.payfee >= bao.money')
            ->where('bao.payfee','neq',0)
            ->count();
        $leftarr['all'] = $leftarr['no'] + $leftarr['bu'] + $leftarr['quan'];
        $data['leftarr'] = $leftarr;


        $data['data'] = PaymentBaoModel::alias('bao')
            ->field('bao.*,e.name as enginname')
            ->leftJoin('engin e','e.id=bao.enginid')
            ->where($where)
            ->where($whereZhu)
            ->where((new MenuService())->get_miss_where('payment_bao', 'bao'))
            ->order('bao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>4,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('payment_bao');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_bao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入报销金额');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }



            $param['number'] = (new PaymentService())->create_payment_bao_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['isturn'] = 1;
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入报销明细');
            }

            Db::startTrans();
            try {
                $id = PaymentBaoModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['baoid'] = $id;
                    PaymentBaoListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'payment_bao',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_bao', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_bao', $id, '项目费用报销');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['budid_name'] = '项目费用';
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_payment_bao_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_bao(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入报销金额');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入报销明细');
            }

            Db::startTrans();
            try {
                PaymentBaoModel::update($param);

                PaymentBaoListModel::where(['baoid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['baoid'] = $param['id'];
                    PaymentBaoListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'payment_bao',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_bao', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_bao', $param['id'], '项目费用报销');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentBaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $data['data']['budid_name'] = '项目费用';

            $list = PaymentBaoListModel::where(['baoid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$vvv){
                $vvv['subname'] = (new BudgetService())->get_subid_quanname($vvv['budid']);
            }
            $data['list'] = $list;


            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('payment_bao', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_bao(Request $request){
        $param = $request->post();
        $data = PaymentBaoModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PaymentBaoModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'payment_bao', $data['id'], '项目费用报销');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_payment_bao(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PaymentBaoModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $data['engininfo'] = $engininfo;

        $jieidinfo = PaymentJieModel::where(['id'=>$data['data']['jieid']])->find();
        if($jieidinfo){
            $accmoney = AccountListModel::where(['type'=>13,'mid'=>$jieidinfo['id']])->sum('money');
            $difee = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
            $difee += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
            $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->column('id');
            $huanfee = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');
            $jieidinfo['daihuan'] = $accmoney - $difee - $huanfee;
        }
        $data['jieidinfo'] = $jieidinfo;


        $list = PaymentBaoListModel::where(['baoid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['data']['accmoney'] = AccountListModel::where(['type'=>4,'mid'=>$data['data']['id']])->sum('money');

        $data['data']['payfo_status'] = (new PaymentService())->get_paydata_paystatus_name($data['data']['money'], $data['data']['payfee']);
        $data['data']['payfo_payfee'] = $data['data']['payfee'];
        $data['data']['payfo_dt'] = AccountListModel::where(['type'=>4,'mid'=>$data['data']['id']])->order('dt DESC')->value('dt');
        $data['data']['payfo_dai'] = $data['data']['money'] - $data['data']['payfee'];

        $data['data']['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $data['data']['incqing']);
        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('payment_bao', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_payment_bao_kuan_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.type','=',4];
        $where[] = ['list.mid','=',$param['mid']];


        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=list.accid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();
        $data['midinfo'] = PaymentBaoModel::where(['id'=>$param['mid']])->find();
        if($data['midinfo']){
            $data['midinfo']['enginname'] = EnginModel::where(['id'=>$data['midinfo']['enginid']])->value('name');
            $data['midinfo']['accmoney'] = AccountListModel::where(['type'=>4,'mid'=>$data['midinfo']['id']])->sum('money');
            $data['midinfo']['daimoney'] = $data['midinfo']['money'] - $data['midinfo']['accmoney'];
        }
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_bao_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $payinfo = PaymentBaoModel::where(['id'=>$param['id']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>4,'mid'=>$payinfo['id']])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['comid'] = $this->userinfo['comid'];
            $accInfo['type'] = 4;
            $accInfo['mid'] = $param['id'];
            $accInfo['enginid'] = $payinfo['enginid'];
            $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            $accInfo['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $accInfo['money'] = $param['fumoney'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['zhai'] = $payinfo['zhai'];
            $accInfo['faer'] = $payinfo['gonger'];
            $accInfo['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payinfo['incdaotype']);
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['bankname'] = $payinfo['bankname'];
            $accInfo['banknum'] = $payinfo['banknum'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['created_user'] = $this->userinfo['id'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['created_at'] = date("Y-m-d H:i:s");
            $accInfo['updated_at'] = date("Y-m-d H:i:s");



            Db::startTrans();
            try {
                PaymentBaoModel::where(['id'=>$param['id']])->update(['incqing'=>$param['incqing']]);

                $id = AccountListModel::insertGetId($accInfo);

                $payfee = AccountListModel::where(['type'=>4,'mid'=>$param['id']])->sum('money');
                PaymentBaoModel::where(['id'=>$param['id']])->update(['payfee'=>$payfee]);



                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentBaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
            $data['data']['accmoney'] = AccountListModel::where(['type'=>4,'mid'=>$data['data']['id']])->sum('money');
            $data['data']['nomoney'] = round($data['data']['money'] - $data['data']['accmoney'], 2);

            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;
            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_all_xiala();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_payment_bao_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AccountListModel::where(['id'=>$param['id']])->find();
            $payinfo = PaymentBaoModel::where(['id'=>$old['mid']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>4,'mid'=>$payinfo['id']])->where('id','neq',$param['id'])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }


            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['id'] = $param['id'];
            $accInfo['enginid'] = $payinfo['enginid'];
            $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            $accInfo['money'] = $param['fumoney'];
            $accInfo['dt'] = $param['dt'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['updated_at'] = date("Y-m-d H:i:s");


            Db::startTrans();
            try {
                PaymentBaoModel::where(['id'=>$payinfo['id']])->update(['incqing'=>$param['incqing']]);

                AccountListModel::update($accInfo);

                $payfee = AccountListModel::where(['type'=>4,'mid'=>$payinfo['id']])->sum('money');
                PaymentBaoModel::where(['id'=>$payinfo['id']])->update(['payfee'=>$payfee]);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $payment = PaymentBaoModel::where(['id'=>$data['data']['mid']])->find();

            $payment['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payment['incdaotype']);
            $payment['accmoney'] = AccountListModel::where(['type'=>4,'mid'=>$payment['id']])->sum('money');
            $payment['nomoney'] = $payment['money'] - $payment['accmoney'];
            $data['payment'] = $payment;

            $engininfo = EnginModel::get_info_by_pk($payment['enginid']);
            $data['engininfo'] = $engininfo;

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;
            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_all_xiala();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_bao_kuan(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }


    public function list_payment_gong(Request $request){
        $param = $request->post();
        get_page($param);

        $whereZhu[] = ['gong.deleted_user','=', 0];
        $whereZhu[] = ['gong.comid','=',$this->userinfo['comid']];
        $whereZhu[] = ['gong.isturn','=',1];
        $whereZhu[] = ['gong.status','in',[0,1]];

        $where = [];
        if(isset($param['my']) && $param['my'] > 0){
            $where[] = ['gong.created_user','=', $this->userinfo['id']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['gong.number|gong.zhai|gong.money|gong.gonger|gong.banknum|gong.bankname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['gong.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['gong.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['gong.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['gong.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['gong.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['gong.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['gong.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['gong.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['gong.status','=', $param['status']];
        }

        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';



        if(isset($param['payss']) && !empty($param['payss'])){
            if($param['payss'] == 'no'){
                $where[] = ['gong.payfee','=', 0];
            }
            if($param['payss'] == 'bu'){
                $where[] = ['gong.payfee','neq', 0];
                $where[] = Db::raw('gong.payfee < gong.money');
            }
            if($param['payss'] == 'quan'){
                $where[] = ['gong.payfee','neq', 0];
                $where[] = Db::raw('gong.payfee >= gong.money');
            }
        }
        $leftarr = [];
        $leftarr['no'] = PaymentGongModel::alias('gong')
            ->field('gong.*')
            ->where($whereZhu)
            ->where((new MenuService())->get_miss_where('payment_gong', 'gong'))
            ->where('gong.payfee','=',0)
            ->count();
        $leftarr['bu'] = PaymentGongModel::alias('gong')
            ->field('gong.*')
            ->where($whereZhu)
            ->where((new MenuService())->get_miss_where('payment_gong', 'gong'))
            ->where('gong.payfee < gong.money')
            ->where('gong.payfee','neq',0)
            ->count();
        $leftarr['quan'] = PaymentGongModel::alias('gong')
            ->field('gong.*')
            ->where($whereZhu)
            ->where((new MenuService())->get_miss_where('payment_gong', 'gong'))
            ->where('gong.payfee >= gong.money')
            ->where('gong.payfee','neq',0)
            ->count();
        $leftarr['all'] = $leftarr['no'] + $leftarr['bu'] + $leftarr['quan'];
        $data['leftarr'] = $leftarr;

        $data['data'] = PaymentGongModel::alias('gong')
            ->field('gong.*')
            ->where($where)
            ->where($whereZhu)
            ->where((new MenuService())->get_miss_where('payment_gong', 'gong'))
            ->order('gong.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>15,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('payment_gong');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_gong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入报销金额');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }



            $param['number'] = (new PaymentService())->create_payment_gong_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['isturn'] = 1;
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入报销明细');
            }

            Db::startTrans();
            try {
                $id = PaymentGongModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['gongid'] = $id;
                    PaymentGongListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'payment_gong',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_gong', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_gong', $id, '公司费用报销');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['budid_name'] = '公司费用';
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_payment_gong_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_gong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入报销金额');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入报销明细');
            }

            Db::startTrans();
            try {
                PaymentGongModel::update($param);

                PaymentGongListModel::where(['gongid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['gongid'] = $param['id'];
                    PaymentGongListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'payment_gong',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_gong', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_gong', $param['id'], '公司费用报销');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentGongModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['data']['budid_name'] = '公司费用';

            $list = PaymentGongListModel::where(['gongid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$vvv){
                $vvv['subname'] = (new BudgetService())->get_subid_quanname($vvv['budid']);
            }
            $data['list'] = $list;


            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('payment_gong', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_gong(Request $request){
        $param = $request->post();
        $data = PaymentGongModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PaymentGongModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'payment_gong', $data['id'], '公司费用报销');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_payment_gong(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PaymentGongModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);

        $jieidinfo = PaymentJieModel::where(['id'=>$data['data']['jieid']])->find();
        if($jieidinfo){
            $accmoney = AccountListModel::where(['type'=>13,'mid'=>$jieidinfo['id']])->sum('money');
            $difee = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
            $difee += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->sum('difee');
            $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$jieidinfo['id']])->column('id');
            $huanfee = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');
            $jieidinfo['daihuan'] = $accmoney - $difee - $huanfee;
        }
        $data['jieidinfo'] = $jieidinfo;

        $list = PaymentGongListModel::where(['gongid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;


        $data['data']['accmoney'] = AccountListModel::where(['type'=>15,'mid'=>$data['data']['id']])->sum('money');


        $data['data']['payfo_status'] = (new PaymentService())->get_paydata_paystatus_name($data['data']['money'], $data['data']['payfee']);
        $data['data']['payfo_payfee'] = $data['data']['payfee'];
        $data['data']['payfo_dt'] = AccountListModel::where(['type'=>15,'mid'=>$data['data']['id']])->order('dt DESC')->value('dt');
        $data['data']['payfo_dai'] = $data['data']['money'] - $data['data']['payfee'];

        $data['data']['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $data['data']['incqing']);
        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('payment_gong', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_payment_gong_kuan_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.type','=',15];
        $where[] = ['list.mid','=',$param['mid']];


        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=list.accid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();

        $data['midinfo'] = PaymentGongModel::where(['id'=>$param['mid']])->find();
        if($data['midinfo']){
            $data['midinfo']['accmoney'] = AccountListModel::where(['type'=>15,'mid'=>$data['midinfo']['id']])->sum('money');
            $data['midinfo']['daimoney'] = $data['midinfo']['money'] - $data['midinfo']['accmoney'];
        }
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_gong_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $payinfo = PaymentGongModel::where(['id'=>$param['id']])->find();


            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>15,'mid'=>$payinfo['id']])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['comid'] = $this->userinfo['comid'];
            $accInfo['type'] = 15;
            $accInfo['mid'] = $param['id'];
            $accInfo['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $accInfo['money'] = $param['fumoney'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['zhai'] = $payinfo['zhai'];
            $accInfo['faer'] = $payinfo['gonger'];
            $accInfo['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payinfo['incdaotype']);
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['bankname'] = $payinfo['bankname'];
            $accInfo['banknum'] = $payinfo['banknum'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['created_user'] = $this->userinfo['id'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['created_at'] = date("Y-m-d H:i:s");
            $accInfo['updated_at'] = date("Y-m-d H:i:s");



            Db::startTrans();
            try {
                PaymentGongModel::where(['id'=>$param['id']])->update(['incqing'=>$param['incqing']]);

                $id = AccountListModel::insertGetId($accInfo);


                $payfee = AccountListModel::where(['type'=>15,'mid'=>$param['id']])->sum('money');
                PaymentGongModel::where(['id'=>$param['id']])->update(['payfee'=>$payfee]);

                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentGongModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;
            $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
            $data['data']['accmoney'] = AccountListModel::where(['type'=>15,'mid'=>$data['data']['id']])->sum('money');
            $data['data']['nomoney'] = round($data['data']['money'] - $data['data']['accmoney'], 2);

            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_payment_gong_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AccountListModel::where(['id'=>$param['id']])->find();
            $payinfo = PaymentGongModel::where(['id'=>$old['mid']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>15,'mid'=>$payinfo['id']])->where('id','neq',$param['id'])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }


            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['id'] = $param['id'];
            $accInfo['money'] = $param['fumoney'];
            $accInfo['dt'] = $param['dt'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['updated_at'] = date("Y-m-d H:i:s");


            Db::startTrans();
            try {
                PaymentGongModel::where(['id'=>$payinfo['id']])->update(['incqing'=>$param['incqing']]);

                AccountListModel::update($accInfo);

                $payfee = AccountListModel::where(['type'=>15,'mid'=>$payinfo['id']])->sum('money');
                PaymentGongModel::where(['id'=>$payinfo['id']])->update(['payfee'=>$payfee]);


                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $payment = PaymentGongModel::where(['id'=>$data['data']['mid']])->find();
            $payment['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payment['incdaotype']);
            $payment['accmoney'] = AccountListModel::where(['type'=>15,'mid'=>$payment['id']])->sum('money');
            $payment['nomoney'] = $payment['money'] - $payment['accmoney'];
            $data['payment'] = $payment;


            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;


            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;
            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_gong_kuan(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }


    public function list_payment_day(Request $request){
        $param = $request->post();
        get_page($param);

        $whereZhu[] = ['day.deleted_user','=', 0];
        $whereZhu[] = ['day.comid','=',$this->userinfo['comid']];
        $whereZhu[] = ['day.isturn','=',1];
        $whereZhu[] = ['day.status','in',[0,1]];

        $where = [];

        if(isset($param['my']) && $param['my'] > 0){
            $where[] = ['day.created_user','=', $this->userinfo['id']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['day.number|day.zhai|day.money|day.gonger|day.banknum|day.bankname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['day.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['day.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['day.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['day.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['day.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['day.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['day.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['day.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['day.status','=', $param['status']];
        }

        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';



        if(isset($param['payss']) && !empty($param['payss'])){
            if($param['payss'] == 'no'){
                $where[] = ['day.payfee','=', 0];
            }
            if($param['payss'] == 'bu'){
                $where[] = ['day.payfee','neq', 0];
                $where[] = Db::raw('day.payfee < day.money');
            }
            if($param['payss'] == 'quan'){
                $where[] = ['day.payfee','neq', 0];
                $where[] = Db::raw('day.payfee >= day.money');
            }
        }
        $leftarr = [];
        $leftarr['no'] = PaymentDayModel::alias('day')
            ->field('day.*')
            ->where($whereZhu)
            ->where((new MenuService())->get_miss_where('payment_day', 'day'))
            ->where('day.payfee','=',0)
            ->count();
        $leftarr['bu'] = PaymentDayModel::alias('day')
            ->field('day.*')
            ->where($whereZhu)
            ->where((new MenuService())->get_miss_where('payment_day', 'day'))
            ->where('day.payfee < day.money')
            ->where('day.payfee','neq',0)
            ->count();
        $leftarr['quan'] = PaymentDayModel::alias('day')
            ->field('day.*')
            ->where($whereZhu)
            ->where((new MenuService())->get_miss_where('payment_day', 'day'))
            ->where('day.payfee >= day.money')
            ->where('day.payfee','neq',0)
            ->count();
        $leftarr['all'] = $leftarr['no'] + $leftarr['bu'] + $leftarr['quan'];
        $data['leftarr'] = $leftarr;

        $data['data'] = PaymentDayModel::alias('day')
            ->field('day.*')
            ->where($whereZhu)
            ->where($where)
            ->where((new MenuService())->get_miss_where('payment_day', 'day'))
            ->order('day.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>5,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('payment_day');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_day(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入请款金额');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }



            $param['number'] = (new PaymentService())->create_payment_day_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['isturn'] = 1;
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入付款明细');
            }

            Db::startTrans();
            try {
                $id = PaymentDayModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['dayid'] = $id;
                    PaymentDayListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'payment_day',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_day', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_day', $id, '日常付款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['budid_name'] = '公司费用';
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_payment_day_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_day(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入请款金额');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入报销明细');
            }

            Db::startTrans();
            try {
                PaymentDayModel::update($param);

                PaymentDayListModel::where(['dayid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['dayid'] = $param['id'];
                    PaymentDayListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'payment_day',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_day', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_day', $param['id'], '日常付款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentDayModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['budid_name'] = '公司费用';

            $list = PaymentDayListModel::where(['dayid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$vvv){
                $vvv['subname'] = (new BudgetService())->get_subid_quanname($vvv['budid']);
            }
            $data['list'] = $list;


            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('payment_day', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_day(Request $request){
        $param = $request->post();
        $data = PaymentDayModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PaymentDayModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'payment_day', $data['id'], '日常付款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_payment_day(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PaymentDayModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);

        $list = PaymentDayListModel::where(['dayid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['data']['accmoney'] = AccountListModel::where(['type'=>5,'mid'=>$data['data']['id']])->sum('money');


        $data['data']['payfo_status'] = (new PaymentService())->get_paydata_paystatus_name($data['data']['money'], $data['data']['payfee']);
        $data['data']['payfo_payfee'] = $data['data']['payfee'];
        $data['data']['payfo_dt'] = AccountListModel::where(['type'=>5,'mid'=>$data['data']['id']])->order('dt DESC')->value('dt');
        $data['data']['payfo_dai'] = $data['data']['money'] - $data['data']['payfee'];

        $data['data']['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $data['data']['incqing']);
        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('payment_day', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_payment_day_kuan_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.type','=',5];
        $where[] = ['list.mid','=',$param['mid']];


        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=list.accid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();

        $data['midinfo'] = PaymentDayModel::where(['id'=>$param['mid']])->find();
        if($data['midinfo']){
            $data['midinfo']['accmoney'] = AccountListModel::where(['type'=>5,'mid'=>$data['midinfo']['id']])->sum('money');
            $data['midinfo']['daimoney'] = $data['midinfo']['money'] - $data['midinfo']['accmoney'];
        }
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_day_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $payinfo = PaymentDayModel::where(['id'=>$param['id']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>5,'mid'=>$payinfo['id']])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['comid'] = $this->userinfo['comid'];
            $accInfo['type'] = 5;
            $accInfo['mid'] = $param['id'];
            $accInfo['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $accInfo['money'] = $param['fumoney'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['zhai'] = $payinfo['zhai'];
            $accInfo['faer'] = $payinfo['gonger'];
            $accInfo['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payinfo['incdaotype']);
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['bankname'] = $payinfo['bankname'];
            $accInfo['banknum'] = $payinfo['banknum'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['created_user'] = $this->userinfo['id'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['created_at'] = date("Y-m-d H:i:s");
            $accInfo['updated_at'] = date("Y-m-d H:i:s");



            Db::startTrans();
            try {
                PaymentDayModel::where(['id'=>$param['id']])->update(['incqing'=>$param['incqing']]);

                $id = AccountListModel::insertGetId($accInfo);

                $payfee = AccountListModel::where(['type'=>5,'mid'=>$param['id']])->sum('money');
                PaymentDayModel::where(['id'=>$param['id']])->update(['payfee'=>$payfee]);

                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentDayModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;
            $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
            $data['data']['accmoney'] = AccountListModel::where(['type'=>5,'mid'=>$data['data']['id']])->sum('money');
            $data['data']['nomoney'] = round($data['data']['money'] - $data['data']['accmoney'], 2);
            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();

            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_payment_day_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AccountListModel::where(['id'=>$param['id']])->find();
            $payinfo = PaymentDayModel::where(['id'=>$old['mid']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>5,'mid'=>$payinfo['id']])->where('id','neq',$param['id'])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }


            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['id'] = $param['id'];
            $accInfo['money'] = $param['fumoney'];
            $accInfo['dt'] = $param['dt'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['updated_at'] = date("Y-m-d H:i:s");


            Db::startTrans();
            try {
                PaymentDayModel::where(['id'=>$payinfo['id']])->update(['incqing'=>$param['incqing']]);

                AccountListModel::update($accInfo);

                $payfee = AccountListModel::where(['type'=>5,'mid'=>$payinfo['id']])->sum('money');
                PaymentDayModel::where(['id'=>$payinfo['id']])->update(['payfee'=>$payfee]);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $payment = PaymentDayModel::where(['id'=>$data['data']['mid']])->find();
            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;
            $payment['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payment['incdaotype']);
            $payment['accmoney'] = AccountListModel::where(['type'=>5,'mid'=>$payment['id']])->sum('money');
            $payment['nomoney'] = $payment['money'] - $payment['accmoney'];
            $data['payment'] = $payment;

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;
            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_day_kuan(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }


    public function list_payment_neikou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kou.deleted_user','=', 0];
        $where[] = ['kou.comid','=',$this->userinfo['comid']];
        $where[] = ['kou.isturn','=',1];
        $where[] = ['kou.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kou.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|kou.number|kou.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['kou.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = PaymentNeikouModel::alias('kou')
            ->field('kou.*,e.name as enginname')
            ->leftJoin('engin e','e.id=kou.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('payment_neikou', 'kou'))
            ->order('kou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['neiid'] = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$item['enginid'],'isturn'=>1,'status'=>1])->value('neiid');
                if($item['neiid']){
                    $neiInfo = ContractNeiModel::where(['id'=>$item['neiid']])->find();
                    $item['nei_name'] = $neiInfo['name'];
                    $item['nei_dutyer'] = $neiInfo['dutyer'];
                }
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('payment_neikou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_neikou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入扣款名称');
            }
            $exist = ContractShiModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$param['enginid']])->find();
            if(!$exist){
                return outputJson(4000, '该项目暂无施工合同,无法提交');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择核算日期');
            }


            $ins['number'] = (new PaymentService())->create_payment_neikou_number();
            $ins['comid'] = $this->userinfo['comid'];
            $ins['isturn'] = 1;
            $ins['status'] = 1;
            $ins['created_user'] = $ins['updated_user'] = $this->userinfo['id'];
            $ins['created_at'] = $ins['updated_at']= date('Y-m-d H:i:s');
            $ins['deptid'] = $this->userinfo['deptid'];
            $ins['deptid'] = $this->userinfo['deptid'];
            $ins['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入扣款明细');
            }


            $ins['enginid'] = $param['enginid'];
            $ins['money'] = $param['money'];
            $ins['dt'] = $param['dt'];
            $ins['zhai'] = $param['zhai'];
            $ins['explain'] = $param['explain'];

            Db::startTrans();
            try {
                $id = PaymentNeikouModel::insertGetId($ins);

                foreach($list as $vv){
                    $vv['kouid'] = $id;
                    PaymentNeikouListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'payment_neikou',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_neikou', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_neikou', $id, '内包扣款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['kouname'] = (new OptService())->get_base_info('engin_neikouname');
            $data['number'] = (new PaymentService())->create_payment_neikou_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_neikou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $old = PaymentNeikouModel::where(['id'=>$param['id']])->find();

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入扣款名称');
            }
            $exist = ContractShiModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$old['enginid']])->find();
            if(!$exist){
                return outputJson(4000, '该项目暂无施工合同,无法提交');
            }

            if(empty($param['dt'])){
                return outputJson(4000, '请选择核算日期');
            }


            $upd['updated_user'] = $this->userinfo['id'];
            $upd['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入扣款明细');
            }


            $upd['id'] = $param['id'];
            $upd['money'] = $param['money'];
            $upd['dt'] = $param['dt'];
            $upd['zhai'] = $param['zhai'];
            $upd['explain'] = $param['explain'];

            Db::startTrans();
            try {
                PaymentNeikouModel::update($upd);

                PaymentNeikouListModel::where(['kouid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['kouid'] = $param['id'];
                    PaymentNeikouListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'payment_neikou',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_neikou', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_neikou', $param['id'], '内包扣款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentNeikouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $neiid = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$data['data']['enginid'],'isturn'=>1,'status'=>1])->value('neiid');
            if($neiid){
                $neiInfo = ContractNeiModel::where(['id'=>$neiid])->find();
                $engininfo['nei_name'] = $neiInfo['name'];
                $engininfo['nei_dutyer'] = $neiInfo['dutyer'];
            }
            $data['engininfo'] = $engininfo;

            $list = PaymentNeikouListModel::where(['kouid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$vvv){
                $listinfo = ContractNeiListModel::where(['id'=>$vvv['listid']])->find();
                $vvv['feiname_name'] = (new OptService())->get_base_showname('engin_nei_feiname', $listinfo['feiname']);
                $vvv['yingkou'] = (new ContractService())->get_contract_nei_feivalue($listinfo, $engininfo['id']);
                $vvv['leikou'] = PaymentNeikouListModel::alias('list')
                    ->join('payment_neikou kou','kou.id=list.kouid')
                    ->where(['kou.comid'=>$this->userinfo['comid'],'kou.deleted_user'=>0,'list.listid'=>$listinfo['id']])
                    ->sum('list.money');
            }
            $data['list'] = $list;
            $data['data']['balance'] = (new EnginService())->get_engin_balance($engininfo['id']);
            $data['kouname'] = (new OptService())->get_base_info('engin_neikouname');
            $data['files'] = (new OptService())->get_file('payment_neikou', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_neikou(Request $request){
        $param = $request->post();
        $data = PaymentNeikouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PaymentNeikouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'payment_neikou', $data['id'], '内包扣款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_payment_neikou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PaymentNeikouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $neiid = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$data['data']['enginid'],'isturn'=>1,'status'=>1])->value('neiid');
        if($neiid){
            $neiInfo = ContractNeiModel::where(['id'=>$neiid])->find();
            $engininfo['nei_name'] = $neiInfo['name'];
            $engininfo['nei_dutyer'] = $neiInfo['dutyer'];
        }
        $engininfo['neiid'] = $neiid;
        $data['engininfo'] = $engininfo;


        $list = PaymentNeikouListModel::where(['kouid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$vvv){
            $listinfo = ContractNeiListModel::where(['id'=>$vvv['listid']])->find();
            $vvv['feiname_name'] = (new OptService())->get_base_showname('engin_nei_feiname', $listinfo['feiname']);
            $vvv['yingkou'] = (new ContractService())->get_contract_nei_feivalue($listinfo, $engininfo['id']);
            $vvv['leikou'] = PaymentNeikouListModel::alias('list')
                ->join('payment_neikou kou','kou.id=list.kouid')
                ->where(['kou.comid'=>$this->userinfo['comid'],'kou.deleted_user'=>0,'list.listid'=>$listinfo['id']])
                ->sum('list.money');
        }
        $data['list'] = $list;
        $data['data']['balance'] = (new EnginService())->get_engin_balance($engininfo['id']);

        $data['data']['leikou'] = PaymentNeikouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid']])->sum('money');


        $data['files'] = (new OptService())->get_file('payment_neikou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_payment_neifu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['fu.deleted_user','=', 0];
        $where[] = ['fu.comid','=',$this->userinfo['comid']];
        $where[] = ['fu.isturn','=',1];
        $where[] = ['fu.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['fu.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|fu.number|fu.zhai|fu.money|fu.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['fu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['fu.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['fu.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['fu.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['fu.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['fu.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['fu.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['fu.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['fu.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $data['data'] = PaymentNeifuModel::alias('fu')
            ->field('fu.*,e.name as enginname')
            ->leftJoin('engin e','e.id=fu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('payment_neifu', 'fu'))
            ->order('fu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['neiid'] = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$item['enginid'],'isturn'=>1,'status'=>1])->value('neiid');
                if($item['neiid']){
                    $neiInfo = ContractNeiModel::where(['id'=>$item['neiid']])->find();
                    $item['nei_name'] = $neiInfo['name'];
                    $item['nei_dutyer'] = $neiInfo['dutyer'];
                }
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>6,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('payment_neifu');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_neifu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            $exist = ContractShiModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$param['enginid']])->find();
            if(!$exist){
                return outputJson(4000, '该项目暂无施工合同,无法提交');
            }

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入请款金额');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }



            $param['number'] = (new PaymentService())->create_payment_neifu_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['isturn'] = 1;
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = PaymentNeifuModel::insertGetId($param);
                (new OptService())->relation_file($files,'payment_neifu',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'payment_neifu', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'payment_neifu', $id, '内包付款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_payment_neifu_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_payment_neifu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $old = PaymentNeifuModel::where(['id'=>$param['id']])->find();

            $exist = ContractShiModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$old['enginid']])->find();
            if(!$exist){
                return outputJson(4000, '该项目暂无施工合同,无法提交');
            }

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入请款金额');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                PaymentNeifuModel::update($param);
                (new OptService())->relation_file($files,'payment_neifu',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'payment_neifu', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'payment_neifu', $param['id'], '内包付款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentNeifuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $neiid = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$data['data']['enginid'],'isturn'=>1,'status'=>1])->value('neiid');
            if($neiid){
                $neiInfo = ContractNeiModel::where(['id'=>$neiid])->find();
                $engininfo['nei_name'] = $neiInfo['name'];
                $engininfo['nei_dutyer'] = $neiInfo['dutyer'];
            }
            $engininfo['incomefee'] = (new EnginService())->get_engin_incomeengin($engininfo['id']);
            $engininfo['balance'] = (new EnginService())->get_engin_balance($engininfo['id']);
            $data['engininfo'] = $engininfo;

            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('payment_neifu', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_neifu(Request $request){
        $param = $request->post();
        $data = PaymentNeifuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PaymentNeifuModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'payment_neifu', $data['id'], '内包付款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_payment_neifu(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PaymentNeifuModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $neiid = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$data['data']['enginid'],'isturn'=>1,'status'=>1])->value('neiid');
        if($neiid){
            $neiInfo = ContractNeiModel::where(['id'=>$neiid])->find();
            $engininfo['nei_name'] = $neiInfo['name'];
            $engininfo['nei_dutyer'] = $neiInfo['dutyer'];
        }
        $engininfo['incomefee'] = (new EnginService())->get_engin_incomeengin($engininfo['id']);
        $engininfo['balance'] = (new EnginService())->get_engin_balance($engininfo['id']);
        $data['engininfo'] = $engininfo;


        $data['data']['accmoney'] = AccountListModel::where(['type'=>6,'mid'=>$data['data']['id']])->sum('money');

        $data['data']['payfo_status'] = (new PaymentService())->get_paydata_paystatus_name($data['data']['money'], $data['data']['payfee']);
        $data['data']['payfo_payfee'] = $data['data']['payfee'];
        $data['data']['payfo_dt'] = AccountListModel::where(['type'=>6,'mid'=>$data['data']['id']])->order('dt DESC')->value('dt');
        $data['data']['payfo_dai'] = $data['data']['money'] - $data['data']['payfee'];


        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $gckshou = (new BudgetService())->get_budshou_sons($gckid);
        $dzkid = (new BudgetService())->get_budshou_pid_by_name('项目收入','垫资款');
        $dzkshou = (new BudgetService())->get_budshou_sons($dzkid);
        $budshou = array_merge($gckshou, $dzkshou);


        $data['data']['lei_huikuan'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid']])->where('budid','in',$budshou)->sum('money');
        $data['data']['lei_payment_engin'] = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid'],'status'=>1,'isturn'=>1])->sum('money');
        $data['data']['lei_payment_bao'] = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid'],'status'=>1,'isturn'=>1])->sum('money');
        $data['data']['lei_payment_neikou'] = PaymentNeikouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid']])->sum('money');
        $data['data']['lei_payment_neifu'] = PaymentNeifuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid'],'status'=>1,'isturn'=>1])->sum('money');
        $data['data']['balance'] = $data['data']['lei_huikuan'] - $data['data']['lei_payment_engin'] - $data['data']['lei_payment_bao']
            - $data['data']['lei_payment_neikou']- $data['data']['lei_payment_neifu'];


        $data['data']['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $data['data']['incqing']);
        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('payment_neifu', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_payment_neifu_kuan(Request $request){
        $param = $request->post();
        get_page($param);

        $whereZhu[] = ['fu.deleted_user','=', 0];
        $whereZhu[] = ['fu.comid','=',$this->userinfo['comid']];
        $whereZhu[] = ['fu.isturn','=',1];
        $whereZhu[] = ['fu.status','in',[0,1]];

        $where = [];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['fu.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|fu.number|fu.zhai|fu.money|fu.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['fu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['fu.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['fu.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['fu.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['fu.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['fu.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['fu.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['fu.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['fu.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        if(isset($param['payss']) && !empty($param['payss'])){
            if($param['payss'] == 'no'){
                $where[] = ['fu.payfee','=', 0];
            }
            if($param['payss'] == 'bu'){
                $where[] = ['fu.payfee','neq', 0];
                $where[] = Db::raw('fu.payfee < fu.money');
            }
            if($param['payss'] == 'quan'){
                $where[] = ['fu.payfee','neq', 0];
                $where[] = Db::raw('fu.payfee >= fu.money');
            }
        }
        $leftarr = [];
        $leftarr['no'] = PaymentNeifuModel::alias('fu')
            ->field('fu.*,e.name as enginname')
            ->leftJoin('engin e','e.id=fu.enginid')
            ->where((new MenuService())->get_miss_where('payment_neifu_kuan', 'e'))
            ->where($whereZhu)
            ->where('fu.payfee','=',0)
            ->count();
        $leftarr['bu'] = PaymentNeifuModel::alias('fu')
            ->field('fu.*,e.name as enginname')
            ->leftJoin('engin e','e.id=fu.enginid')
            ->where((new MenuService())->get_miss_where('payment_neifu_kuan', 'e'))
            ->where($whereZhu)
            ->where('fu.payfee < fu.money')
            ->where('fu.payfee','neq',0)
            ->count();
        $leftarr['quan'] = PaymentNeifuModel::alias('fu')
            ->field('fu.*,e.name as enginname')
            ->leftJoin('engin e','e.id=fu.enginid')
            ->where((new MenuService())->get_miss_where('payment_neifu_kuan', 'e'))
            ->where($whereZhu)
            ->where('fu.payfee >= fu.money')
            ->where('fu.payfee','neq',0)
            ->count();
        $leftarr['all'] = $leftarr['no'] + $leftarr['bu'] + $leftarr['quan'];
        $data['leftarr'] = $leftarr;


        $data['data'] = PaymentNeifuModel::alias('fu')
            ->field('fu.*,e.name as enginname')
            ->leftJoin('engin e','e.id=fu.enginid')
            ->where($whereZhu)
            ->where($where)
            ->where((new MenuService())->get_miss_where('payment_neifu_kuan', 'e'))
            ->order('fu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                $neiid = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$item['enginid'],'isturn'=>1,'status'=>1])->value('neiid');
                if($neiid){
                    $neiInfo = ContractNeiModel::where(['id'=>$neiid])->find();
                    $item['nei_name'] = $neiInfo['name'];
                    $item['nei_dutyer'] = $neiInfo['dutyer'];
                }
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>6,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('payment_neifu_kuan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_payment_neifu_kuan_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.type','=',6];
        $where[] = ['list.mid','=',$param['mid']];


        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=list.accid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();

        $data['midinfo'] = PaymentNeifuModel::where(['id'=>$param['mid']])->find();
        if($data['midinfo']){
            $data['midinfo']['enginname'] = EnginModel::where(['id'=>$data['midinfo']['enginid']])->value('name');
            $data['midinfo']['accmoney'] = AccountListModel::where(['type'=>6,'mid'=>$data['midinfo']['id']])->sum('money');
            $data['midinfo']['daimoney'] = $data['midinfo']['money'] - $data['midinfo']['accmoney'];
        }
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_neifu_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $payinfo = PaymentNeifuModel::where(['id'=>$param['id']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>6,'mid'=>$payinfo['id']])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);

            $accInfo = [];
            $accInfo['comid'] = $this->userinfo['comid'];
            $accInfo['type'] = 6;
            $accInfo['mid'] = $param['id'];
            $accInfo['enginid'] = $payinfo['enginid'];
            $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            $accInfo['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $accInfo['money'] = $param['fumoney'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['zhai'] = $payinfo['zhai'];
            $accInfo['faer'] = $payinfo['gonger'];
            $accInfo['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payinfo['incdaotype']);
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['bankname'] = $payinfo['bankname'];
            $accInfo['banknum'] = $payinfo['banknum'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['created_user'] = $this->userinfo['id'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['created_at'] = date("Y-m-d H:i:s");
            $accInfo['updated_at'] = date("Y-m-d H:i:s");



            Db::startTrans();
            try {
                PaymentNeifuModel::where(['id'=>$param['id']])->update(['incqing'=>$param['incqing']]);

                $id = AccountListModel::insertGetId($accInfo);

                $payfee = AccountListModel::where(['type'=>6,'mid'=>$param['id']])->sum('money');
                PaymentNeifuModel::where(['id'=>$param['id']])->update(['payfee'=>$payfee]);

                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentNeifuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $neiid = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$data['data']['enginid'],'isturn'=>1,'status'=>1])->value('neiid');
            if($neiid){
                $neiInfo = ContractNeiModel::where(['id'=>$neiid])->find();
                $engininfo['nei_name'] = $neiInfo['name'];
                $engininfo['nei_dutyer'] = $neiInfo['dutyer'];
            }
            $engininfo['incomefee'] = (new EnginService())->get_engin_incomeengin($engininfo['id']);
            $engininfo['balance'] = (new EnginService())->get_engin_balance($engininfo['id']);
            $data['engininfo'] = $engininfo;

            $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
            $data['data']['accmoney'] = AccountListModel::where(['type'=>6,'mid'=>$data['data']['id']])->sum('money');
            $data['data']['nomoney'] = round($data['data']['money'] - $data['data']['accmoney'], 2);

            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_all_xiala();

            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_payment_neifu_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AccountListModel::where(['id'=>$param['id']])->find();
            $payinfo = PaymentNeifuModel::where(['id'=>$old['mid']])->find();


            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['incqing'])){
                return outputJson(4000, '请选择发票情况');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>6,'mid'=>$payinfo['id']])->where('id','neq',$param['id'])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }


            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['id'] = $param['id'];
            $accInfo['enginid'] = $payinfo['enginid'];
            $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            $accInfo['money'] = $param['fumoney'];
            $accInfo['dt'] = $param['dt'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['updated_at'] = date("Y-m-d H:i:s");


            Db::startTrans();
            try {
                PaymentNeifuModel::where(['id'=>$payinfo['id']])->update(['incqing'=>$param['incqing']]);

                AccountListModel::update($accInfo);

                $payfee = AccountListModel::where(['type'=>6,'mid'=>$payinfo['id']])->sum('money');
                PaymentNeifuModel::where(['id'=>$payinfo['id']])->update(['payfee'=>$payfee]);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $payment = PaymentNeifuModel::where(['id'=>$data['data']['mid']])->find();
            $payment['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payment['incdaotype']);
            $payment['accmoney'] = AccountListModel::where(['type'=>6,'mid'=>$payment['id']])->sum('money');
            $payment['nomoney'] = $payment['money'] - $payment['accmoney'];
            $data['payment'] = $payment;

            $engininfo = EnginModel::get_info_by_pk($payment['enginid']);
            $neiid = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$payment['enginid'],'isturn'=>1,'status'=>1])->value('neiid');
            if($neiid){
                $neiInfo = ContractNeiModel::where(['id'=>$neiid])->find();
                $engininfo['nei_name'] = $neiInfo['name'];
                $engininfo['nei_dutyer'] = $neiInfo['dutyer'];
            }
            $engininfo['incomefee'] = (new EnginService())->get_engin_incomeengin($engininfo['id']);
            $engininfo['balance'] = (new EnginService())->get_engin_balance($engininfo['id']);
            $data['engininfo'] = $engininfo;

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;


            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;

            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_neifu_kuan(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }



    public function list_bid_bond_jiao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jiao.deleted_user','=', 0];
        $where[] = ['jiao.comid','=',$this->userinfo['comid']];
        $where[] = ['jiao.isturn','=',1];
        $where[] = ['jiao.status','in',[0,1]];
        $where[] = ['jiao.bidid','gt', 0];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['e.id','=', $param['bidid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|jiao.number|jiao.zhai|jiao.money|jiao.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','eq',$param['business']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','eq', $param['state']];
        }
        if(isset($param['bond_type']) && !empty($param['bond_type'])){
            $where[] = ['e.bond_type','eq', $param['bond_type']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['e.status','=', $param['status']];
        }

        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jiao.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['jiao.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jiao.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['jiao.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['jiao.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['jiao.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['jiao.bankname','like', '%'.$param['bankname'].'%'];
        }

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';



        $data['data'] = EnginBondJiaoModel::alias('jiao')
            ->field('jiao.*,e.name as enginname')
            ->leftJoin('bid e','e.id=jiao.bidid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_bond_jiao', 'jiao'))
            ->order('jiao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['bondtype'] = (new PaymentService())->get_engin_bondtype_name($item['bondtype']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>7,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                $item['tuimoney'] = AccountListModel::where(['type'=>8,'mid'=>$item['id']])->sum('money');
                $item['daituimoney'] = $item['money'] - $item['tuimoney'];

                $item['state'] = '未支付';
                if(AccountListModel::where(['type'=>7,'mid'=>$item['id']])->find()){
                    $item['state'] = '已支付';
                }
                if(AccountListModel::where(['type'=>8,'mid'=>$item['id']])->find()){
                    $item['state'] = '已退回';
                }

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('bid_bond_jiao');
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['bond_type'] = BidModel::get_bond_type();
        $data['state'] = (new BidService())->get_bid_state();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_bond_jiao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jiao.deleted_user','=', 0];
        $where[] = ['jiao.comid','=',$this->userinfo['comid']];
        $where[] = ['jiao.isturn','=',1];
        $where[] = ['jiao.status','in',[0,1]];
        $where[] = ['jiao.enginid','gt',0];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jiao.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|jiao.number|jiao.zhai|jiao.money|jiao.gonger','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jiao.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['bondtype']) && !empty($param['bondtype'])){
            $where[] = ['jiao.bondtype','=', $param['bondtype']];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['jiao.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jiao.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['jiao.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['jiao.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['jiao.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['jiao.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jiao.status','=', $param['status']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
        $data['is_bondtype'] = isset($param['bondtype']) ? $param['bondtype'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';



        $data['data'] = EnginBondJiaoModel::alias('jiao')
            ->field('jiao.*,e.name as enginname')
            ->leftJoin('engin e','e.id=jiao.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_bond_jiao', 'jiao'))
            ->order('jiao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['bondtype'] = (new PaymentService())->get_engin_bondtype_name($item['bondtype']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>7,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                $item['tuimoney'] = AccountListModel::where(['type'=>8,'mid'=>$item['id']])->sum('money');
                $item['daituimoney'] = $item['money'] - $item['tuimoney'];

                $item['state'] = '未支付';
                if(AccountListModel::where(['type'=>7,'mid'=>$item['id']])->find()){
                    $item['state'] = '已支付';
                }
                if(AccountListModel::where(['type'=>8,'mid'=>$item['id']])->find()){
                    $item['state'] = '已退回';
                }
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_bond_jiao');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_bond_jiao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['bondtype'] == 1){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择项目名称');
                }
            }else{
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
            }

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入请款金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }



            $param['number'] = (new PaymentService())->create_bond_jiao_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['isturn'] = 1;
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = EnginBondJiaoModel::insertGetId($param);
                (new OptService())->relation_file($files,'engin_bond_jiao',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_bond_jiao', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_bond_jiao', $id, '项目保证金缴纳');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_bond_jiao_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_bond_jiao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入请款金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择支付方式');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginBondJiaoModel::update($param);
                (new OptService())->relation_file($files,'engin_bond_jiao',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_bond_jiao', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_bond_jiao', $param['id'], '项目保证金缴纳');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondJiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['bond_enddt'] = substr($data['data']['bond_enddt'],0,16);

            if($data['data']['enginid'] > 0){
                $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            }else{
                $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
            }
            $data['engininfo'] = $engininfo;

            $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('engin_bond_jiao', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_bond_jiao(Request $request){
        $param = $request->post();
        $data = EnginBondJiaoModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginBondJiaoModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_bond_jiao', $data['id'], '项目保证金缴纳');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_bond_jiao(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginBondJiaoModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['bond_enddt'] = substr($data['data']['bond_enddt'],0,16);

        if($data['data']['enginid'] > 0){
            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        }else{
            $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
        }
        $data['engininfo'] = $engininfo;

        $data['data']['accmoney'] = AccountListModel::where(['type'=>7,'mid'=>$data['data']['id']])->sum('money');
        $data['data']['tuimoney'] = AccountListModel::where(['type'=>8,'mid'=>$data['data']['id']])->sum('money');


        $data['data']['payfee'] = AccountListModel::where(['type'=>7,'mid'=>$data['data']['id']])->sum('money');
        $data['data']['payfo_tuifee'] = AccountListModel::where(['type'=>8,'mid'=>$data['data']['id']])->sum('money');
        $data['data']['payfo_status'] = (new PaymentService())->get_paydata_paystatus_name($data['data']['money'], $data['data']['payfee']);
        $data['data']['payfo_payfee'] = $data['data']['payfee'];
        $data['data']['payfo_dt'] = AccountListModel::where(['type'=>7,'mid'=>$data['data']['id']])->order('dt DESC')->value('dt');
        $data['data']['payfo_tuidt'] = AccountListModel::where(['type'=>8,'mid'=>$data['data']['id']])->order('dt DESC')->value('dt');
        $data['data']['payfo_dai'] = $data['data']['money'] - $data['data']['payfee'];


        $data['data']['bondtype'] = (new PaymentService())->get_engin_bondtype_name($data['data']['bondtype']);
        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('engin_bond_jiao', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_engin_bond_jiao_kuan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jiao.deleted_user','=', 0];
        $where[] = ['jiao.comid','=',$this->userinfo['comid']];
        $where[] = ['jiao.isturn','=',1];
        $where[] = ['jiao.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jiao.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|jiao.number|jiao.zhai|jiao.money|jiao.gonger','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name|bid.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number|bid.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jiao.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['bondtype']) && !empty($param['bondtype'])){
            $where[] = ['jiao.bondtype','=', $param['bondtype']];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['jiao.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jiao.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['jiao.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['jiao.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['jiao.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['jiao.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jiao.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
        $data['is_bondtype'] = isset($param['bondtype']) ? $param['bondtype'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';



        $data['data'] = EnginBondJiaoModel::alias('jiao')
            ->field('jiao.*,e.name as enginname,bid.name as bidname')
            ->leftJoin('engin e','e.id=jiao.enginid')
            ->leftJoin('bid bid','bid.id=jiao.bidid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_bond_jiao_kuan', 'jiao'))
            ->order('jiao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }
                $item['bondtype'] = (new PaymentService())->get_engin_bondtype_name($item['bondtype']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>7,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                $item['tuimoney'] = AccountListModel::where(['type'=>8,'mid'=>$item['id']])->sum('money');
                $item['daituimoney'] = $item['money'] - $item['tuimoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_bond_jiao_kuan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_bond_jiao_kuan_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.type','in',[7,8]];
        $where[] = ['list.mid','=',$param['mid']];


        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=list.accid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['type'] == 7){
                    $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                }else{
                    $item['budid_name'] = (new BudgetService())->get_budid_quanname($item['budid']);
                }

                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();

        $data['midinfo'] = EnginBondJiaoModel::where(['id'=>$param['mid']])->find();
        if($data['midinfo']){
            $data['midinfo']['enginname'] = EnginModel::where(['id'=>$data['midinfo']['enginid']])->value('name');
            $data['midinfo']['bondtype'] = (new PaymentService())->get_engin_bondtype_name($data['midinfo']['bondtype']);

            $data['midinfo']['accmoney'] = AccountListModel::where(['type'=>7,'mid'=>$data['midinfo']['id']])->sum('money');
            $data['midinfo']['daimoney'] = $data['midinfo']['money'] - $data['midinfo']['accmoney'];
            $data['midinfo']['tuimoney'] = AccountListModel::where(['type'=>8,'mid'=>$data['midinfo']['id']])->sum('money');
            $data['midinfo']['daituimoney'] = $data['midinfo']['money'] - $data['midinfo']['tuimoney'];

        }
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_bond_jiao_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $payinfo = EnginBondJiaoModel::where(['id'=>$param['id']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>7,'mid'=>$payinfo['id']])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);

            $accInfo = [];
            $accInfo['comid'] = $this->userinfo['comid'];
            $accInfo['type'] = 7;
            $accInfo['mid'] = $param['id'];
            $accInfo['bidid'] = $payinfo['bidid'];
            $accInfo['enginid'] = $payinfo['enginid'];
            if($payinfo['bidid'] > 0){
                $accInfo['enginname'] = BidModel::where(['id'=>$payinfo['bidid']])->value('name');
            }else{
                $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            }
            $accInfo['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $accInfo['money'] = $param['fumoney'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['zhai'] = $payinfo['zhai'];
            $accInfo['faer'] = $payinfo['gonger'];
            $accInfo['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payinfo['incdaotype']);
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['bankname'] = $payinfo['bankname'];
            $accInfo['banknum'] = $payinfo['banknum'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['created_user'] = $this->userinfo['id'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['created_at'] = date("Y-m-d H:i:s");
            $accInfo['updated_at'] = date("Y-m-d H:i:s");



            Db::startTrans();
            try {
                $id = AccountListModel::insertGetId($accInfo);

                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondJiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            if($data['data']['enginid'] > 0){
                $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            }else{
                $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
            }
            $data['engininfo'] = $engininfo;

            $data['data']['bondtype'] = (new PaymentService())->get_engin_bondtype_name($data['data']['bondtype']);
            $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
            $data['data']['accmoney'] = AccountListModel::where(['type'=>7,'mid'=>$data['data']['id']])->sum('money');
            $data['data']['nomoney'] = round($data['data']['money'] - $data['data']['accmoney'], 2);


            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_engin_bond_jiao_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AccountListModel::where(['id'=>$param['id']])->find();
            $payinfo = EnginBondJiaoModel::where(['id'=>$old['mid']])->find();


            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>7,'mid'=>$payinfo['id']])->where('id','neq',$param['id'])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }


            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['id'] = $param['id'];
            $accInfo['bidid'] = $payinfo['bidid'];
            $accInfo['enginid'] = $payinfo['enginid'];
            if($payinfo['bidid'] > 0){
                $accInfo['enginname'] = BidModel::where(['id'=>$payinfo['bidid']])->value('name');
            }else{
                $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            }
            $accInfo['money'] = $param['fumoney'];
            $accInfo['dt'] = $param['dt'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['updated_at'] = date("Y-m-d H:i:s");


            Db::startTrans();
            try {
                AccountListModel::update($accInfo);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $payment = EnginBondJiaoModel::where(['id'=>$data['data']['mid']])->find();
            $payment['bondtype'] = (new PaymentService())->get_engin_bondtype_name($payment['bondtype']);
            $payment['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payment['incdaotype']);
            $payment['accmoney'] = AccountListModel::where(['type'=>7,'mid'=>$payment['id']])->sum('money');
            $payment['nomoney'] = $payment['money'] - $payment['accmoney'];
            $data['payment'] = $payment;

            if($payment['enginid'] > 0){
                $engininfo = EnginModel::get_info_by_pk($payment['enginid']);
            }else{
                $engininfo = BidModel::get_info_by_pk($payment['bidid']);
            }
            $data['engininfo'] = $engininfo;

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;
            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_bond_jiao_kuan(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function add_engin_bond_tui_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $payinfo = EnginBondJiaoModel::where(['id'=>$param['id']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次收款金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);

            $accInfo = [];
            $accInfo['comid'] = $this->userinfo['comid'];
            $accInfo['type'] = 8;
            $accInfo['bidid'] = $payinfo['bidid'];
            $accInfo['enginid'] = $payinfo['enginid'];
            if($payinfo['bidid'] > 0){
                $accInfo['enginname'] = BidModel::where(['id'=>$payinfo['bidid']])->value('name');
            }else{
                $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            }
            $accInfo['mid'] = $param['id'];
            $accInfo['jiaonum'] = (new IncomeService())->create_account_list_jiaonum_shou();
            $accInfo['money'] = $param['fumoney'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['zhai'] = $payinfo['zhai'];
            $accInfo['faer'] = $payinfo['gonger'];
            $accInfo['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $param['incdaotype']);
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['bankname'] = $payinfo['bankname'];
            $accInfo['banknum'] = $payinfo['banknum'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['created_user'] = $this->userinfo['id'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['created_at'] = date("Y-m-d H:i:s");
            $accInfo['updated_at'] = date("Y-m-d H:i:s");



            Db::startTrans();
            try {
                $id = AccountListModel::insertGetId($accInfo);

                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondJiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            if($data['data']['bidid'] > 0){
                $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
            }else{
                $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            }

            $data['engininfo'] = $engininfo;

            $data['data']['bondtype'] = (new PaymentService())->get_engin_bondtype_name($data['data']['bondtype']);

            $data['subarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['jiaonum'] = (new IncomeService())->create_account_list_jiaonum_shou();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_engin_bond_tui_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AccountListModel::where(['id'=>$param['id']])->find();
            $payinfo = EnginBondJiaoModel::where(['id'=>$old['mid']])->find();


            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次收款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['id'] = $param['id'];
            $accInfo['bidid'] = $payinfo['bidid'];
            $accInfo['enginid'] = $payinfo['enginid'];
            if($payinfo['bidid'] > 0){
                $accInfo['enginname'] = BidModel::where(['id'=>$payinfo['bidid']])->value('name');
            }else{
                $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            }
            $accInfo['money'] = $param['fumoney'];
            $accInfo['dt'] = $param['dt'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['updated_at'] = date("Y-m-d H:i:s");


            Db::startTrans();
            try {
                AccountListModel::update($accInfo);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $payment = EnginBondJiaoModel::where(['id'=>$data['data']['mid']])->find();
            $payment['bondtype'] = (new PaymentService())->get_engin_bondtype_name($payment['bondtype']);
            $data['payment'] = $payment;

            if($payment['bidid'] > 0){
                $engininfo = BidModel::get_info_by_pk($payment['bidid']);
            }else{
                $engininfo = EnginModel::get_info_by_pk($payment['enginid']);
            }
            $data['engininfo'] = $engininfo;

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            if($data['data']['type'] == 7){
                $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            }else{
                $budinfo = EnginBudshouModel::where(['id'=>$data['data']['budid']])->find();
            }

            $data['budinfo'] = $budinfo;

            $data['subarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();
            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }




    public function list_bid_bond_kai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','in',[0,1]];
        $where[] = ['kai.bidid','gt', 0];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|kai.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','eq',$param['business']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','eq', $param['state']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['bondtype']) && !empty($param['bondtype'])){
            $where[] = ['kai.bondtype','=', $param['bondtype']];
        }
        if(isset($param['hantype']) && !empty($param['hantype'])){
            $where[] = ['kai.hantype','=', $param['hantype']];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['kai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['kai.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['beidaner']) && !empty($param['beidaner'])){
            $where[] = ['kai.beidaner','like', '%'.$param['beidaner'].'%'];
        }
        if(isset($param['daner']) && !empty($param['daner'])){
            $where[] = ['kai.daner','like', '%'.$param['daner'].'%'];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['kai.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['xiaodt']) && !empty($param['xiaodt'])){
            $where[] = ['kai.xiaodt','between', explode(' - ', $param['xiaodt'])];
        }
        if(isset($param['shoufee']) && !empty($param['shoufee'])){
            $where[] = ['kai.shoufee','like', '%'.$param['shoufee'].'%'];
        }


        $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
        $data['is_bondtype'] = isset($param['bondtype']) ? $param['bondtype'] : '';

        $data['hantype'] = (new OptService())->get_base_info('engin_bond_hantype');
        $data['is_hantype'] = isset($param['hantype']) ? $param['hantype'] : '';


        $data['state'] = (new BidService())->get_bid_state();
        $data['data'] = EnginBondKaiModel::alias('kai')
            ->field('kai.*,e.name as enginname,e.business,e.state,e.isqi')
            ->leftJoin('bid e','e.id=kai.bidid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_bond_kai', 'kai'))
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['created_user'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['bondtype'] = (new PaymentService())->get_engin_bondtype_name($item['bondtype']);
                $item['hantype'] = (new OptService())->get_base_showname('engin_bond_hantype', $item['hantype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('bid_bond_kai');
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_bond_kai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','in',[0,1]];
        $where[] = ['kai.enginid','gt',0];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|kai.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['bondtype']) && !empty($param['bondtype'])){
            $where[] = ['kai.bondtype','=', $param['bondtype']];
        }
        if(isset($param['hantype']) && !empty($param['hantype'])){
            $where[] = ['kai.hantype','=', $param['hantype']];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['kai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['kai.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['beidaner']) && !empty($param['beidaner'])){
            $where[] = ['kai.beidaner','like', '%'.$param['beidaner'].'%'];
        }
        if(isset($param['daner']) && !empty($param['daner'])){
            $where[] = ['kai.daner','like', '%'.$param['daner'].'%'];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['kai.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['xiaodt']) && !empty($param['xiaodt'])){
            $where[] = ['kai.xiaodt','between', explode(' - ', $param['xiaodt'])];
        }
        if(isset($param['shoufee']) && !empty($param['shoufee'])){
            $where[] = ['kai.shoufee','like', '%'.$param['shoufee'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
        $data['is_bondtype'] = isset($param['bondtype']) ? $param['bondtype'] : '';

        $data['hantype'] = (new OptService())->get_base_info('engin_bond_hantype');
        $data['is_hantype'] = isset($param['hantype']) ? $param['hantype'] : '';



        $data['data'] = EnginBondKaiModel::alias('kai')
            ->field('kai.*,e.name as enginname')
            ->leftJoin('engin e','e.id=kai.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_bond_kai', 'kai'))
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['bondtype'] = (new PaymentService())->get_engin_bondtype_name($item['bondtype']);
                $item['hantype'] = (new OptService())->get_base_showname('engin_bond_hantype', $item['hantype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_bond_kai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_bond_kai_cai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|kai.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name|bid.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number|bid.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['bondtype']) && !empty($param['bondtype'])){
            $where[] = ['kai.bondtype','=', $param['bondtype']];
        }
        if(isset($param['hantype']) && !empty($param['hantype'])){
            $where[] = ['kai.hantype','=', $param['hantype']];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['kai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['kai.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['beidaner']) && !empty($param['beidaner'])){
            $where[] = ['kai.beidaner','like', '%'.$param['beidaner'].'%'];
        }
        if(isset($param['daner']) && !empty($param['daner'])){
            $where[] = ['kai.daner','like', '%'.$param['daner'].'%'];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['kai.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['xiaodt']) && !empty($param['xiaodt'])){
            $where[] = ['kai.xiaodt','between', explode(' - ', $param['xiaodt'])];
        }
        if(isset($param['shoufee']) && !empty($param['shoufee'])){
            $where[] = ['kai.shoufee','like', '%'.$param['shoufee'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
        $data['is_bondtype'] = isset($param['bondtype']) ? $param['bondtype'] : '';

        $data['hantype'] = (new OptService())->get_base_info('engin_bond_hantype');
        $data['is_hantype'] = isset($param['hantype']) ? $param['hantype'] : '';



        $data['data'] = EnginBondKaiModel::alias('kai')
            ->field('kai.*,e.name as enginname,bid.name as bidname')
            ->leftJoin('engin e','e.id=kai.enginid')
            ->leftJoin('bid bid','bid.id=kai.bidid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_bond_kai_cai', 'kai'))
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }
                $item['bondtype'] = (new PaymentService())->get_engin_bondtype_name($item['bondtype']);
                $item['hantype'] = (new OptService())->get_base_showname('engin_bond_hantype', $item['hantype']);
                $item['accmoney'] = AccountListModel::where(['type'=>7,'mid'=>$item['id']])->sum('money');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_bond_kai_cai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_bond_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['bondtype'] == 1){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择项目名称');
                }
            }else{
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
            }
            if(empty($param['hantype'])){
                return outputJson(4000, '请选择保函类别');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入保函金额');
            }




            $param['comid'] = $this->userinfo['comid'];
            $param['isturn'] = 1;
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = EnginBondKaiModel::insertGetId($param);
                (new OptService())->relation_file($files,'engin_bond_kai',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_bond_kai', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_bond_kai', $id, '项目保函开具');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
            $data['hantype'] = (new OptService())->get_base_info('engin_bond_hantype');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_bond_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['hantype'])){
                return outputJson(4000, '请选择保函类别');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入保函金额');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginBondKaiModel::update($param);
                (new OptService())->relation_file($files,'engin_bond_kai',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_bond_kai', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_bond_kai', $param['id'], '项目保函开具');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondKaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['jiedt'] = substr($data['data']['jiedt'],0,16);

            if($data['data']['enginid'] > 0){
                $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            }else{
                $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
            }
            $data['engininfo'] = $engininfo;

            $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
            $data['hantype'] = (new OptService())->get_base_info('engin_bond_hantype');
            $data['files'] = (new OptService())->get_file('engin_bond_kai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function kai_engin_bond_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['kaidt'])){
                return outputJson(4000, '请选择开具日期');
            }
            if(empty($param['xiaodt'])){
                return outputJson(4000, '请选择有效期');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginBondKaiModel::update($param);
                (new OptService())->relation_file($files,'engin_bond_kai',$param['id']);
                (new OptService())->add_flow_log('开具', 'engin_bond_kai', $param['id']);
                (new OptService())->add_log_list('开具', 'engin_bond_kai', $param['id'], '项目保函开具');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondKaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            if($data['data']['enginid'] > 0){
                $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            }else{
                $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
            }
            $data['engininfo'] = $engininfo;

            $data['data']['bondtype'] = (new PaymentService())->get_engin_bondtype_name($data['data']['bondtype']);
            $data['data']['hantype'] = (new OptService())->get_base_showname('engin_bond_hantype', $data['data']['hantype']);
            $data['files'] = (new OptService())->get_file('engin_bond_kai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_bond_kai(Request $request){
        $param = $request->post();
        $data = EnginBondKaiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginBondKaiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_bond_kai', $data['id'], '项目保函开具');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_bond_kai(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginBondKaiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['jiedt'] = substr($data['data']['jiedt'],0,16);

        if($data['data']['enginid'] > 0){
            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        }else{
            $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
        }
        $data['engininfo'] = $engininfo;

        $data['data']['bondtype'] = (new PaymentService())->get_engin_bondtype_name($data['data']['bondtype']);
        $data['data']['hantype'] = (new OptService())->get_base_showname('engin_bond_hantype', $data['data']['hantype']);
        $data['files'] = (new OptService())->get_file('engin_bond_kai', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_engin_bond_fan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['fan.deleted_user','=', 0];
        $where[] = ['fan.comid','=',$this->userinfo['comid']];
        $where[] = ['fan.isturn','=',1];
        $where[] = ['fan.status','in',[0,1]];


         if(isset($param['bid']) && !empty($param['bid'])){
             $where[] = ['bid.id','=', $param['bid']];
         }
        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['bid.id','=', $param['bidid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|bid.name|fan.number|fan.zhai|fan.money|fan.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name|bid.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number|bid.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['fan.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['bondtype']) && !empty($param['bondtype'])){
            $where[] = ['shou.bondtype','=', $param['bondtype']];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['fan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['fan.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['fan.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['fan.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['fan.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['fan.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['fan.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
        $data['is_bondtype'] = isset($param['bondtype']) ? $param['bondtype'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['data'] = EnginBondFanModel::alias('fan')
            ->field('fan.*,e.name as enginname,bid.name as bidname,shou.bondtype,shou.bidid')
            ->leftJoin('engin_bond_shou shou','shou.id=fan.shouid')
            ->leftJoin('engin e','e.id=shou.enginid')
            ->leftJoin('bid bid','bid.id=shou.bidid')

            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_bond_fan', 'fan'))
            ->order('fan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }
                $item['bondtype'] = (new PaymentService())->get_engin_bondtype_name($item['bondtype']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>10,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_bond_fan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_bond_fan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['shouid'])){
                return outputJson(4000, '请选择关联收取');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入请款金额');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }



            $param['number'] = (new PaymentService())->create_bond_fan_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['isturn'] = 1;
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = EnginBondFanModel::insertGetId($param);
                (new OptService())->relation_file($files,'engin_bond_fan',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_bond_fan', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_bond_fan', $id, '项目保证金返还');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new PaymentService())->create_bond_fan_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_bond_fan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['money'])){
                return outputJson(4000, '请输入请款金额');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入付款对象');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginBondFanModel::update($param);
                (new OptService())->relation_file($files,'engin_bond_fan',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_bond_fan', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_bond_fan', $param['id'], '项目保证金返还');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondFanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $shouinfo = EnginBondShouModel::get_info_by_pk($data['data']['shouid']);
            if($shouinfo['bondtype'] == 1){
                $shouinfo['enginname'] = BidModel::where(['id'=>$shouinfo['bidid']])->value('name');
            }else{
                $shouinfo['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
            }

            $shouinfo['bondtype'] = (new PaymentService())->get_engin_bondtype_name($shouinfo['bondtype']);
            $data['shouinfo'] = $shouinfo;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('engin_bond_fan', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_bond_fan(Request $request){
        $param = $request->post();
        $data = EnginBondFanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginBondFanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_bond_fan', $data['id'], '项目保证金返还');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_bond_fan(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginBondFanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $shouinfo = EnginBondShouModel::get_info_by_pk($data['data']['shouid']);
        if($shouinfo['bondtype'] == 1){
            $shouinfo['enginname'] = BidModel::where(['id'=>$shouinfo['bidid']])->value('name');
        }else{
            $shouinfo['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
        }
        $shouinfo['bondtype'] = (new PaymentService())->get_engin_bondtype_name($shouinfo['bondtype']);
        $data['shouinfo'] = $shouinfo;



        $data['data']['payfee'] = AccountListModel::where(['type'=>10,'mid'=>$data['data']['id']])->sum('money');
        $data['data']['payfo_status'] = (new PaymentService())->get_paydata_paystatus_name($data['data']['money'], $data['data']['payfee']);
        $data['data']['payfo_payfee'] = $data['data']['payfee'];
        $data['data']['payfo_dt'] = AccountListModel::where(['type'=>10,'mid'=>$data['data']['id']])->order('dt DESC')->value('dt');
        $data['data']['payfo_dai'] = $data['data']['money'] - $data['data']['payfee'];


        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('engin_bond_fan', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_engin_bond_fan_kuan_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.type','=',10];
        $where[] = ['list.mid','=',$param['mid']];


        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=list.accid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();

        $data['midinfo'] = EnginBondFanModel::where(['id'=>$param['mid']])->find();
        if($data['midinfo']){
            $shouinfo = EnginBondShouModel::where(['id'=>$data['midinfo']['shouid']])->find();
            $data['midinfo']['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
            $data['midinfo']['bondtype'] = (new PaymentService())->get_engin_bondtype_name($shouinfo['bondtype']);
            $data['midinfo']['accmoney'] = AccountListModel::where(['type'=>10,'mid'=>$data['midinfo']['id']])->sum('money');
            $data['midinfo']['daimoney'] = $data['midinfo']['money'] - $data['midinfo']['accmoney'];
        }
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_bond_fan_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $payinfo = EnginBondFanModel::where(['id'=>$param['id']])->find();
            $shouInfo = EnginBondShouModel::where(['id'=>$payinfo['shouid']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>10,'mid'=>$payinfo['id']])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);

            $accInfo = [];
            $accInfo['comid'] = $this->userinfo['comid'];
            $accInfo['type'] = 10;
            $accInfo['mid'] = $param['id'];

            $accInfo['bidid'] = $shouInfo['bidid'];
            $accInfo['enginid'] = $shouInfo['enginid'];
            if($shouInfo['bidid'] > 0){
                $accInfo['enginname'] = BidModel::where(['id'=>$shouInfo['bidid']])->value('name');
            }else{
                $accInfo['enginname'] = EnginModel::where(['id'=>$shouInfo['enginid']])->value('name');
            }

            $accInfo['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $accInfo['money'] = $param['fumoney'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['zhai'] = $payinfo['zhai'];
            $accInfo['faer'] = $payinfo['gonger'];
            $accInfo['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payinfo['incdaotype']);
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['bankname'] = $payinfo['bankname'];
            $accInfo['banknum'] = $payinfo['banknum'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['created_user'] = $this->userinfo['id'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['created_at'] = date("Y-m-d H:i:s");
            $accInfo['updated_at'] = date("Y-m-d H:i:s");



            Db::startTrans();
            try {
                $id = AccountListModel::insertGetId($accInfo);

                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondFanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $shouinfo = EnginBondShouModel::get_info_by_pk($data['data']['shouid']);
            if($shouinfo['bondtype'] == 1){
                $shouinfo['enginname'] = BidModel::where(['id'=>$shouinfo['bidid']])->value('name');
            }else{
                $shouinfo['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
            }
            $shouinfo['bondtype'] = (new PaymentService())->get_engin_bondtype_name($shouinfo['bondtype']);
            $data['shouinfo'] = $shouinfo;

            $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
            $data['data']['accmoney'] = AccountListModel::where(['type'=>10,'mid'=>$data['data']['id']])->sum('money');
            $data['data']['nomoney'] = round($data['data']['money'] - $data['data']['accmoney'], 2);

            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_engin_bond_fan_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AccountListModel::where(['id'=>$param['id']])->find();
            $payinfo = EnginBondFanModel::where(['id'=>$old['mid']])->find();
            $shouInfo = EnginBondShouModel::where(['id'=>$payinfo['shouid']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>10,'mid'=>$payinfo['id']])->where('id','neq',$param['id'])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }


            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['id'] = $param['id'];
            $accInfo['bidid'] = $shouInfo['bidid'];
            $accInfo['enginid'] = $shouInfo['enginid'];
            if($shouInfo['bidid'] > 0){
                $accInfo['enginname'] = BidModel::where(['id'=>$shouInfo['bidid']])->value('name');
            }else{
                $accInfo['enginname'] = EnginModel::where(['id'=>$shouInfo['enginid']])->value('name');
            }
            $accInfo['money'] = $param['fumoney'];
            $accInfo['dt'] = $param['dt'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['updated_at'] = date("Y-m-d H:i:s");


            Db::startTrans();
            try {
                AccountListModel::update($accInfo);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $payment = EnginBondFanModel::where(['id'=>$data['data']['mid']])->find();
            $shouinfo = EnginBondShouModel::where(['id'=>$payment['shouid']])->find();
            if($shouinfo['bondtype'] == 1){
                $payment['enginname'] = BidModel::where(['id'=>$shouinfo['bidid']])->value('name');
            }else{
                $payment['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
            }
            $payment['bondtype'] = (new PaymentService())->get_engin_bondtype_name($shouinfo['bondtype']);
            $payment['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payment['incdaotype']);
            $payment['accmoney'] = AccountListModel::where(['type'=>10,'mid'=>$payment['id']])->sum('money');
            $payment['nomoney'] = $payment['money'] - $payment['accmoney'];
            $data['payment'] = $payment;


            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;
            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_bond_fan_kuan(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }




    public function list_payment_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];

        if(isset($param['my']) && $param['my'] > 0){
            $where[] = ['jie.created_user','=', $this->userinfo['id']];
        }

        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['jie.created_at','between', [$param['year'].'-01-01 00:00:00', $param['year'].'-12-31 23:59:59']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|jie.number|jie.money|jie.gonger|jie.incyong','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['incyong']) && !empty($param['incyong'])){
            $where[] = ['jie.incyong','like', '%'.$param['incyong'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['jie.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['jie.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['jie.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['jie.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';



        $data['data'] = PaymentJieModel::alias('jie')
            ->field('jie.*,e.name as enginname')
            ->leftJoin('engin e','e.id=jie.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('payment_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
               

                $item['accmoney'] = AccountListModel::where(['type'=>13,'mid'=>$item['id']])->sum('money');
                $item['difee'] = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$item['id']])->sum('difee');
                $item['difee'] += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$item['id']])->sum('difee');
                $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$item['id']])->column('id');
                $item['huanfee'] = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');
                $item['daihuan'] = $item['accmoney'] - $item['difee'] - $item['huanfee'];


                return $item;
            })->toArray();




        $data['butauth'] = (new MenuService())->get_butauth('payment_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_payment_jie(Request $request){
        $param = $request->post();
        $data = PaymentJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PaymentJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'payment_jie', $data['id'], '备用金申请');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_payment_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PaymentJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $data['engininfo'] = $engininfo;


        $data['data']['payfee'] = AccountListModel::where(['type'=>13,'mid'=>$data['data']['id']])->sum('money');
        $data['data']['payfo_status'] = (new PaymentService())->get_paydata_paystatus_name($data['data']['money'], $data['data']['payfee']);
        $data['data']['payfo_payfee'] = $data['data']['payfee'];
        $data['data']['payfo_dt'] = AccountListModel::where(['type'=>13,'mid'=>$data['data']['id']])->order('dt DESC')->value('dt');
        $data['data']['payfo_dai'] = $data['data']['money'] - $data['data']['payfee'];


        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('payment_jie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_payment_jie_kuan_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.type','=',13];
        $where[] = ['list.mid','=',$param['mid']];


        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=list.accid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();

        $data['midinfo'] = PaymentJieModel::where(['id'=>$param['mid']])->find();
        if($data['midinfo']){
            $data['midinfo']['accmoney'] = AccountListModel::where(['type'=>13,'mid'=>$data['midinfo']['id']])->sum('money');
            $data['midinfo']['daimoney'] = $data['midinfo']['money'] - $data['midinfo']['accmoney'];

        }
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_payment_jie_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $payinfo = PaymentJieModel::where(['id'=>$param['id']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>13,'mid'=>$payinfo['id']])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);

            $accInfo = [];
            $accInfo['comid'] = $this->userinfo['comid'];
            $accInfo['type'] = 13;
            $accInfo['mid'] = $param['id'];
            $accInfo['enginid'] = $payinfo['enginid'];
            $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            $accInfo['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $accInfo['money'] = $param['fumoney'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['zhai'] = $payinfo['incyong'];
            $accInfo['faer'] = $payinfo['gonger'];
            $accInfo['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payinfo['incdaotype']);
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['bankname'] = $payinfo['bankname'];
            $accInfo['banknum'] = $payinfo['banknum'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['created_user'] = $this->userinfo['id'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['created_at'] = date("Y-m-d H:i:s");
            $accInfo['updated_at'] = date("Y-m-d H:i:s");



            Db::startTrans();
            try {
                $id = AccountListModel::insertGetId($accInfo);

                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PaymentJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
            $data['data']['accmoney'] = AccountListModel::where(['type'=>13,'mid'=>$data['data']['id']])->sum('money');
            $data['data']['nomoney'] = round($data['data']['money'] - $data['data']['accmoney'], 2);

            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_payment_jie_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AccountListModel::where(['id'=>$param['id']])->find();
            $payinfo = PaymentJieModel::where(['id'=>$old['mid']])->find();


            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次付款金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $accmoney = AccountListModel::where(['type'=>13,'mid'=>$payinfo['id']])->where('id','neq',$param['id'])->sum('money');
            $nomoney = round($payinfo['money'] - $accmoney, 2);
            if($param['fumoney'] > $nomoney){
                return outputJson(4000, '本次付款金额不能大于未付金额');
            }


            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['id'] = $param['id'];
            $accInfo['money'] = $param['fumoney'];
            $accInfo['dt'] = $param['dt'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['updated_at'] = date("Y-m-d H:i:s");


            Db::startTrans();
            try {
                AccountListModel::update($accInfo);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $payment = PaymentJieModel::where(['id'=>$data['data']['mid']])->find();
            $payment['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $payment['incdaotype']);
            $payment['accmoney'] = AccountListModel::where(['type'=>13,'mid'=>$payment['id']])->sum('money');
            $payment['nomoney'] = $payment['money'] - $payment['accmoney'];
            $data['payment'] = $payment;


            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $data['subarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $budinfo = EnginBudsubModel::where(['id'=>$data['data']['budid']])->find();
            $data['budinfo'] = $budinfo;
            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_payment_jie_kuan(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }






    public function payment_jie_tai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.status','=',1];
        $where[] = ['jie.isturn','=',1];

        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['jie.gonger','like', '%'.$param['gonger'].'%'];
        }

        $whereNei = [];
        $year = '';
        if(isset($param['year']) && !empty($param['year'])){
            $year = $param['year'];
            $where[] = ['jie.created_at','between', [$param['year'].'-01-01 00:00:00', $param['year'].'-12-31 23:59:59']];
            $whereNei[] = ['created_at','between', [$param['year'].'-01-01 00:00:00', $param['year'].'-12-31 23:59:59']];
        }


        $data['data'] = PaymentJieModel::alias('jie')
            ->field('jie.gonger,sum(jie.money) as lei_jie')
            ->where($where)
            ->where((new MenuService())->get_miss_where('payment_jie_tai', 'e'))
            ->order('jie.id DESC')
            ->group('jie.gonger')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($whereNei, $year){
                $item['year'] = $year;
                $jieidArr = PaymentJieModel::where(['comid'=>$this->userinfo['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where($whereNei)->where(['gonger'=>$item['gonger']])->column('id');

                $item['lei_jiezhi'] = AccountListModel::where(['type'=>13])
                    ->where('mid','in',$jieidArr)->sum('money');

                $item['lei_di'] = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where('jieid','in',$jieidArr)->sum('difee');
                $item['lei_di'] += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where('jieid','in',$jieidArr)->sum('difee');

                $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where('jieid','in',$jieidArr)->column('id');
                $item['huanfee'] = AccountListModel::where(['type'=>14])
                    ->where('mid','in',$huanidArr)->sum('money');

                $item['daihuan'] = $item['lei_jiezhi'] - $item['lei_di'] - $item['huanfee'];


                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function payment_jie_hui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.status','=',1];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.enginid','gt',0];

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }

        $whereNei = [];
        $year = '';
        if(isset($param['year']) && !empty($param['year'])){
            $year = $param['year'];
            $where[] = ['jie.created_at','between', [$param['year'].'-01-01 00:00:00', $param['year'].'-12-31 23:59:59']];
            $whereNei[] = ['created_at','between', [$param['year'].'-01-01 00:00:00', $param['year'].'-12-31 23:59:59']];
        }


        $data['data'] = PaymentJieModel::alias('jie')
            ->field('jie.enginid,sum(jie.money) as lei_jie')
            ->where($where)
            ->where((new MenuService())->get_miss_where('payment_jie_hui', 'e'))
            ->order('jie.id DESC')
            ->group('jie.enginid')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($whereNei, $year){
                $item['enginname'] = EnginModel::where(['id'=>$item['enginid']])->value('name');
                $item['year'] = $year;
                $jieidArr = PaymentJieModel::where(['comid'=>$this->userinfo['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where($whereNei)->where(['enginid'=>$item['enginid']])->column('id');

                $item['lei_jiezhi'] = AccountListModel::where(['type'=>13])
                    ->where('mid','in',$jieidArr)->sum('money');

                $item['lei_di'] = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where('jieid','in',$jieidArr)->sum('difee');
                $item['lei_di'] += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where('jieid','in',$jieidArr)->sum('difee');

                $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where('jieid','in',$jieidArr)->column('id');
                $item['huanfee'] = AccountListModel::where(['type'=>14])
                    ->where('mid','in',$huanidArr)->sum('money');

                $item['daihuan'] = $item['lei_jiezhi'] - $item['lei_di'] - $item['huanfee'];


                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }



    public function list_payment_paydata(Request $request){
        $param = $request->post();
        get_page($param);

        $whereZhu[] = ['pay.comid','=',$this->userinfo['comid']];

        $where = [];
        // 1-未支付 2-部分支付 3-全部支付
        if(isset($param['paystatus']) && !empty($param['paystatus'])){
            if($param['paystatus'] == 1){
                $where[] = ['pay.payfee','=', 0];
            }
            if($param['paystatus'] == 2){
                $where[] = ['pay.payfee','>', 0];
                $where[] = ['pay.payfee','<', 'pay.money'];
            }
            if($param['paystatus'] == 3){
                $where[] = ['pay.payfee','egt', 'pay.money'];
            }
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['pay.number|e.name|pay.gonger|pay.zhai|user.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['pay.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['paytype']) && !empty($param['paytype'])){
            $where[] = ['pay.paytype','=', $param['paytype']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['pay.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['pay.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['pay.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['pay.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['created_user_name']) && !empty($param['created_user_name'])){
            $where[] = ['user.realname','like', '%'.$param['created_user_name'].'%'];
        }

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['pay.status','=', $param['status']];
        }


        $data['paytype'] = (new PaymentService())->get_paydata_paytype();
        $data['is_paytype'] = isset($param['paytype']) ? $param['paytype'] : '';

        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';




        if(isset($param['payss']) && !empty($param['payss'])){
            if($param['payss'] == 'no'){
                $where[] = ['pay.payfee','=', 0];
            }
            if($param['payss'] == 'bu'){
                $where[] = ['pay.payfee','neq', 0];
                $where[] = Db::raw('pay.payfee < pay.money');
            }
            if($param['payss'] == 'quan'){
                $where[] = ['pay.payfee','neq', 0];
                $where[] = Db::raw('pay.payfee >= pay.money');
            }
        }
        $leftarr = [];
        $leftarr['no'] = PaydataModel::alias('pay')
            ->field('pay.*,e.name as enginname,user.realname as created_user_name')
            ->leftJoin('engin e','e.id=pay.enginid')
            ->leftjoin('user user','user.id=pay.created_user')
            ->where((new MenuService())->get_miss_where('payment_paydata', 'e'))
            ->where($whereZhu)
            ->where('pay.payfee','=',0)
            ->count();
        $leftarr['bu'] = PaydataModel::alias('pay')
            ->field('pay.*,e.name as enginname,user.realname as created_user_name')
            ->leftJoin('engin e','e.id=pay.enginid')
            ->leftjoin('user user','user.id=pay.created_user')
            ->where((new MenuService())->get_miss_where('payment_paydata', 'e'))
            ->where($whereZhu)
            ->where('pay.payfee < pay.money')
            ->where('pay.payfee','neq',0)
            ->count();
        $leftarr['quan'] = PaydataModel::alias('pay')
            ->field('pay.*,e.name as enginname,user.realname as created_user_name')
            ->leftJoin('engin e','e.id=pay.enginid')
            ->leftjoin('user user','user.id=pay.created_user')
            ->where((new MenuService())->get_miss_where('payment_paydata', 'e'))
            ->where($whereZhu)
            ->where('pay.payfee >= pay.money')
            ->where('pay.payfee','neq',0)
            ->count();
        $leftarr['all'] = $leftarr['no'] + $leftarr['bu'] + $leftarr['quan'];
        $data['leftarr'] = $leftarr;

        $data['data'] = PaydataModel::alias('pay')
            ->field('pay.*,e.name as enginname,user.realname as created_user_name')
            ->leftJoin('engin e','e.id=pay.enginid')
            ->leftjoin('user user','user.id=pay.created_user')
            ->where((new MenuService())->get_miss_where('payment_paydata', 'e'))
            ->where($where)
            ->where($whereZhu)
            ->order('pay.created_at DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = '';
                if($item['paytype'] == 3){
                    $item['budid_name'] = (new PaymentService())->get_payment_budid_name($item['budid']);
                }
                if($item['paytype'] == 5){
                    $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                }


                $item['paytype_name'] = (new PaymentService())->get_paydata_paytype_name($item['paytype']);
                $item['paystatus'] = (new PaymentService())->get_paydata_paystatus_name($item['money'], $item['payfee']);
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['daimoney'] = $item['money'] - $item['payfee'];
                return $item;
            })->toArray();
        $data['butauth'] = (new MenuService())->get_butauth('payment_paydata');
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_payment_paybao(Request $request){
        $param = $request->post();
        get_page($param);

        $whereZhu[] = ['pay.comid','=',$this->userinfo['comid']];

        $where = [];
        // 1-未支付 2-部分支付 3-全部支付
        if(isset($param['paystatus']) && !empty($param['paystatus'])){
            if($param['paystatus'] == 1){
                $where[] = ['pay.payfee','=', 0];
            }
            if($param['paystatus'] == 2){
                $where[] = ['pay.payfee','>', 0];
                $where[] = ['pay.payfee','<', 'pay.money'];
            }
            if($param['paystatus'] == 3){
                $where[] = ['pay.payfee','egt', 'pay.money'];
            }
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['pay.number|e.name|pay.gonger|pay.zhai|user.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['pay.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['paytype']) && !empty($param['paytype'])){
            $where[] = ['pay.paytype','=', $param['paytype']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['pay.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['pay.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['pay.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['pay.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['created_user_name']) && !empty($param['created_user_name'])){
            $where[] = ['user.realname','like', '%'.$param['created_user_name'].'%'];
        }
        if(isset($param['isdi']) && $param['isdi'] != ''){
            $where[] = ['pay.isdi','=', $param['isdi']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['pay.status','=', $param['status']];
        }


        $data['paytype'] = (new PaymentService())->get_paybao_paytype();
        $data['is_paytype'] = isset($param['paytype']) ? $param['paytype'] : '';

        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        if(isset($param['payss']) && !empty($param['payss'])){
            if($param['payss'] == 'no'){
                $where[] = ['pay.payfee','=', 0];
            }
            if($param['payss'] == 'bu'){
                $where[] = ['pay.payfee','neq', 0];
                $where[] = Db::raw('pay.payfee < pay.money');
            }
            if($param['payss'] == 'quan'){
                $where[] = ['pay.payfee','neq', 0];
                $where[] = Db::raw('pay.payfee >= pay.money');
            }
        }
        $leftarr = [];
        $leftarr['no'] = PaybaoModel::alias('pay')
            ->field('pay.*,e.name as enginname,user.realname as created_user_name')
            ->leftJoin('engin e','e.id=pay.enginid')
            ->leftjoin('user user','user.id=pay.created_user')
            ->where($whereZhu)
            ->where('pay.payfee','=',0)
            ->count();
        $leftarr['bu'] = PaybaoModel::alias('pay')
            ->field('pay.*,e.name as enginname,user.realname as created_user_name')
            ->leftJoin('engin e','e.id=pay.enginid')
            ->leftjoin('user user','user.id=pay.created_user')
            ->where($whereZhu)
            ->where('pay.payfee < pay.money')
            ->where('pay.payfee','neq',0)
            ->count();
        $leftarr['quan'] = PaybaoModel::alias('pay')
            ->field('pay.*,e.name as enginname,user.realname as created_user_name')
            ->leftJoin('engin e','e.id=pay.enginid')
            ->leftjoin('user user','user.id=pay.created_user')
            ->where($whereZhu)
            ->where('pay.payfee >= pay.money')
            ->where('pay.payfee','neq',0)
            ->count();
        $leftarr['all'] = $leftarr['no'] + $leftarr['bu'] + $leftarr['quan'];
        $data['leftarr'] = $leftarr;

        $data['data'] = PaybaoModel::alias('pay')
            ->field('pay.*,e.name as enginname,user.realname as created_user_name')
            ->leftJoin('engin e','e.id=pay.enginid')
            ->leftjoin('user user','user.id=pay.created_user')
            ->where($where)
            ->where($whereZhu)
            ->order('pay.created_at DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = '';
                if($item['paytype'] == 4){
                    $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                }
                if($item['paytype'] == 15){
                    $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                }

                $item['paytype_name'] = (new PaymentService())->get_paybao_paytype_name($item['paytype']);
                $item['paystatus'] = (new PaymentService())->get_paydata_paystatus_name($item['money'], $item['payfee']);
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['daimoney'] = $item['money'] - $item['payfee'];
                return $item;
            })->toArray();
        $data['butauth'] = (new MenuService())->get_butauth('payment_paybao');
        return outputJson(2000,lang('code_2000'),$data);
    }


}
